Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 66

Saurabh Tariyal Sec:J 1918677

Saurabh Tariyal Sec:J 1918677

Assignment 1.
Q1. Write various steps to create SQL user step by step.

Create user new identified by saurabh;


Show user;
Grant resource , connect to new;
Show user

Assignment 2

1. Logon to Oracle by user name given to you.

Enter user-name: practice

Enter password:

2. See list of tables by using “Tab / user_tables / cat” tables.

select * from tab;


Saurabh Tariyal Sec:J 1918677

3. View the user name (Make sure it’s your user name: eg CSA23 etc)

show user

4. View today’s date.

Select sysdate from dual;

5. See the list of all environment variables.


Show all;
Saurabh Tariyal Sec:J 1918677

7. Perform some mathematical operations by using dual. (Addition, multiplication etc.)

Select 12+14 from dual;

Select 45*54 from dual;

8. Create a table “Student” with following attributes :


Field Name Type Size
Rollno Number
Name varchar2 10
City varchar2 10

Create table student (rollno number,name varchar2(15),city var2char(15));

9. Display the structure of table

Desc student;

10. see the records of the table

Select * from student;


Saurabh Tariyal Sec:J 1918677

11. Insert the following data into the table

Insert into student values(1,’amit’,’dehradun’);

Insert into student values(2,’kapil’,’merrut’);

Insert into student values(4,NULL.’dehradun’);

Insert into student values(4,’amit’,NULL);

Insert into student values(5,NULL,’delhi’);

12. see the records of the table.

Select * from student;

13. add a new column named “Email”, type varchar2(15)

Alter table student add(email varchar2(15));


Saurabh Tariyal Sec:J 1918677

14. see the records of the table.

Select * from student;

15. Display the structure of table

Desc student;

16. Set the email of all records to @btech@cse.

Update student set email=’btech@scce’;

17. see the records of the table.

18. roll back the email data

Select * from student;

Rollback;
Saurabh Tariyal Sec:J 1918677

19. see the records of the table.


Select * from student;

20. Display the structure of table

Desc student;

21. Set the email of ‘Amit’ to ‘abc@xyz’

Update student set email=’abc@xyz’ where name=’amit’;

22. see the records of the table

Select * from student;


Saurabh Tariyal Sec:J 1918677

23. Complete all the blank records of your own and add 5 More records (Total = 10).

Update student set email=’Kapil@abc’ where rollno=2;

`update student set name=’ram’,email=’ram@cse’ where rollno=3;

Update student set name=’mohit’,email=’mohit@cse’ where rollno=5;

Insert into student values (&rollno,’&name,’&city’,’&email’);


Saurabh Tariyal Sec:J 1918677

24. commit data & clear the screen (clear screen)

Commit;

25. see all records.

Select * from student;


Saurabh Tariyal Sec:J 1918677

26. set linesize 20

27. again see all records. (note the display style)

Set linesize 20;

Select * from student;


Saurabh Tariyal Sec:J 1918677
28. see the structure of table (is it same as displayed in question no. 20? )

Desc student;

29. let linesize 500

30. again see all records. (note the display style)

Set linesize 500;

Select * from student;

31. set pagesize 7

32. again see all records. (note the display style)

Set pagesize 7;

Select * from student;


Saurabh Tariyal Sec:J 1918677

33. set pagesize 200

34. again see all records. (note the display style)

Set pagesize 200;

Select * from student;

35. set heading off

36. again see all records. (note the display style)


Set heading off;

Select * from student;


Saurabh Tariyal Sec:J 1918677

37. set heading on


38. again see all records. (note the display style)

Set heading on;


Select * from student;

39. set pause on


40. again see all records. ( if not displayed , press enter , see records once more)

Set pause on;


Select * from student;
Saurabh Tariyal Sec:J 1918677

41. Remove the name column

Alter table student drop column name;

42. Display the structure

Desc student;

43. See the records.


Select * from student;

44. Make a duplicate copy of Student table with the name “Duplicate”

Create table copy as select * from student;


Saurabh Tariyal Sec:J 1918677

46. Truncate the “Student” table.


47. Rollback and see the records.

Truncate table student;


Rollback;
Select * from student;

48. Drop the STUDENT table.

Drop table student;

49. See the records of dropped table.

Select * from student;


Saurabh Tariyal Sec:J 1918677

50. See the list of all tables. ( Is your table present?)

Rollback;

Select * from student;

Select * from tab;


Saurabh Tariyal Sec:J 1918677

DBMS Practical List – 2


Platform : Oracle

1.Logon to Oracle by user name given to you.


2.See list of tables by using “Tab”
Conn;
select * from tab;

3.View the user name, make sure it’s your login name.

show user;
Saurabh Tariyal Sec:J 1918677

4.Create a new table “Employee” with following attributes/fields :


Field Name Data Type Size of column

EmpCode Number
Name varchar2 6 (Six)
Salary Number

create table employee(empcode number,name varchar2(15),salary


number);

5.Display the structure of table

Desc employee;

6.See the records of the table


Select * from employee;
Saurabh Tariyal Sec:J 1918677

7.Insert the following data into the table using any method:

EmpCode Name Salary


101 Amit 3000
201 Kapil 2200
301 Rohit 4500
401 Amit 5500
401 Amit 6500

insert into employee values(101,'amit',3000);


insert into employee values(201,'kapil',2200);
insert into employee values(301,'mohit',4500);
insert into employee values(401,'aman',5500);
insert into employee values(501,'anuj',6500);
commit;
Saurabh Tariyal Sec:J 1918677

8.See the records of the table.

Select * from employee;

9.Try to Add the following new record

EmpCode Name Salary


901 amit kumar 3300
sajwaan
Note the output, even if it is error!
insert into employee values(901,'amit kumar sajwaan',3300);

10. See the structure of table and note the size of name column
Desc employee;

11. Increase the size of name column to 25.


alter table employee modify(name varchar2(25));
Saurabh Tariyal Sec:J 1918677

12. See the structure of table again.


Desc employee;

13. See the records of table.


select * from employee;

14. Now again try to add the above record again,


insert into employee values(901,'amit kumar sajwaan',3300);

15. See the records of the table

select * from employee;


Saurabh Tariyal Sec:J 1918677

16. Reduce the size of name column to 5. (Note the output even if it is an error)
alter table employee modify(name varchar2(5));

17. Commit the data and see all records.

Commit;
Saurabh Tariyal Sec:J 1918677
Saurabh Tariyal Sec:J 1918677

18. Execute the following SQL queries and note the result:
a) Select name from employee;

b) Select name, name, name, name from employee;

c) Select name, empcode from employee;


Saurabh Tariyal Sec:J 1918677

d) Select name, empcode from employee order by salary;

e) Select name, Salary, salary + 500 , salary-50000 from employee;

f) Select sum(salary) from employee;


Saurabh Tariyal Sec:J 1918677

g) Select min(salary) from employee;

h) Select max(salary) from employee;

19. Display name, actual salary, 50 % increased salary of all employees.


select name , salary,salary+salary*0.5 from employee;

20. Make a copy of employee table as “NewEmp” table with all records.
create table newemp as select * from employee;
Saurabh Tariyal Sec:J 1918677

21. See the records of “NewEmp” table.


select * from newemp;
Saurabh Tariyal Sec:J 1918677

Make a copy of employee table as “Emp2” table with no (Zero) records.


create table emp2 as select * from employee where empcode <0;

22. See the records of all 3 table (Employee, NewEmp and Emp2).
select * from employee;
select * from newemp;
select * from emp2;
Saurabh Tariyal Sec:J 1918677

23. See the structure of all 3 table (Employee, NewEmp and Emp2).
Desc employee;
Desc newemp;
Desc emp2;

24. Create a new table “MyTable” from employee table with only Name and Salary
columns.
create table mytable as select name, salary from employee;

25. See the records of Employee Table and MyTable.


select * from mytable;
select * from employee;
Saurabh Tariyal Sec:J 1918677

26. See the list of all tables.


select * from tab;

27. Commit your data.


Commit;

28. Drop the columns “name” and “empcode” from NewEmp table.
alter table newemp drop(name,empcode);

29. See the records of NewEmp Table.


select * from newemp;
Saurabh Tariyal Sec:J 1918677

30. Truncate the “Mytable” table. (do not use delete command).
truncate table mytable;
Saurabh Tariyal Sec:J 1918677

31. See the records of “MyTable” table.


select * from mytable;

32. Rollback.
Rollback;

33. Again See the records of “MyTable” table. (What is your observation?)
select * from mytable;

34. Drop the “MyTable” table.


drop table mytable;

35. See the records of “MyTable” table.


select * from mytable;

36. Rollback.
Rollback;
Saurabh Tariyal Sec:J 1918677
Saurabh Tariyal Sec:J 1918677

37. Again See the records of “MyTable” table. (What is your observation?)
select * from mytable;

38. See the list of all tables in our login.


39. Commit the data.
select * from tab;
commit;
Saurabh Tariyal Sec:J 1918677

Assignment 3

Write queries for the following outputs.

1. Show all records

2. Show record of all students living in Delhi

3.Show record of all student whose name starts with “A”.


Saurabh Tariyal Sec:J 1918677

4.Show record of all student whose second alphabet of name is “a”.

5.Show record of all student whose name ends with “it”.

6.Show records of all students having age greater than 25 & living in Dehradun.
Saurabh Tariyal Sec:J 1918677

7.Show the list of all cities (names of cities should not be repeated)

8.Show the names students alphabetically in ascending order.


Saurabh Tariyal Sec:J 1918677

9.Display name in lowercase, cities

in uppercase.

10.Display those records which do not have pin code

=>Insert the following data further into the same table.

Roll No Name City Pincode Age


12 Gaurav Rampur 312125
Manish 314136
Saurabh Tariyal Sec:J 1918677

14 Aviral 319143 29
15 Gwaliar 313149 25
11.A. Assign Roll No. “13” to Manish.
11.B. Assign the name “Abhijeet” to Roll No. 15
11.C. Increase all age by 3 years.
11.D. Set the age of all students living in Meerut to “25”.

12. Add a new column named “Balance”, type number to the Students table.
Saurabh Tariyal Sec:J 1918677

13. Set the balance of all students to Rs. 20,000/-. See All records. Commit.
Saurabh Tariyal Sec:J 1918677

14. Increase the balance by Rs. 500/- for all Bombay & Delhi students. See All records.
Commit.

15.Show the total balance amount of all students.


Saurabh Tariyal Sec:J 1918677

16.Show the maximum balance

17.Create a new table “student2” as a copy of Students with all rows from Students table

Practical List – 5 Constraints-1


Saurabh Tariyal Sec:J 1918677

Course: B Tech

1. WAQ (write a ans) to create an “Account” table with following constraints:

All constraints to be given ONLY at column level


AccountNo Number Primary Key

CustName Varchar2 Not Null

AccountType Varchar2 Default Value ‘Saving’

Balance Number >1000

PassportNo Varchar2 Unique

Ans : create table Account(AccountNo number primary key,CustName varchar2(15) NOT


NULL,AccountType varchar2(15) default 'Saving',Balance Number check (balance>1000),passportno
varchar2(15) Unique) 

2. See and note the structure of above created table using “describe”.

and write answers for the following questions:

3. See the contents of “User_Constraints” table and note the constraint number and type
Saurabh Tariyal Sec:J 1918677

of constraints for your above table.

4. Insert the following data in the above table (In case of Error , Correct the data yourself)

117501 Puneet Saving 1100 B9895

117502 Saving 2100 A9895


117507 Rohit Current 500 C9898

117507 Rohit Current 5000 C9898


117504 Kumar Saving 1100 C9898

117508 Shweta Saving 7100 A6789


117503 Deepak Current 12000

Ans:

 insert into Account values (117501,'puneet','Saving',1100,'B9895');


 insert into Account values (117507,'rohit','current',5000,'C9898');
 insert into Account values (117509,'aman','current',5500,'C8898');
 insert into Account values (117504,'kumar','saving',1100,'C9897');
 insert into Account values (117508,'sweta','saving',7100,'A6789');
 insert into Account values (117503,'deepak','current',12000,'A6P89');

Output:
Saurabh Tariyal Sec:J 1918677

5. WAQ (write a ans) to create an “Account2” table with following constraints:

AccountNo Number Primary Key

CustName Varchar2 Not Null

AccountType Varchar2 Default Value ‘Current’

Balance Number >5000

PassportNo Varchar2 Unique

Ans:create table Account2(AccountNo number primary key,CustName varchar2(15) NOT


NULL,AccountType varchar2(15) default 'Current',Balance Number check
(balance>5000),passportno varchar2(15) Unique);

6. See and note the structure of above created table using “describe”. (Note structure carefully)

Output:

:
Saurabh Tariyal Sec:J 1918677

7. WAQ (write a ans) to create an “Account3” table with following constraints:

AccountNo Number Primary Key (at Row/Table Level)

CustName Varchar2 Not Null (Only at Table / Row Level)

Ans:create table Account3(AccountNo number,CustName varchar2(15) not null,primary


key(AccountNo));

8. See the contents of “User_Constraints” table and note the constraint number and type

of constraints for your above tables.

Output:

9. Drop the NOT NULL Constraint from Account3 Table.

ans:alter table account3 drop constraint SYS_C007455;

10. WAQ (write a ans) to create an “Account4” table with following constraints:

AccountNo Number Primary Key (Constraint Name : PKey)

CustName Varchar2 Not Null (Constraint Name : NNull )

AccountType Varchar2 Default Value ‘Current’

Balance Number >5000 (Constraint Name : ChkBal)

PassportNo Varchar2 Unique (Constraint Name : UU)


Saurabh Tariyal Sec:J 1918677

Ans :create table Account4(AccountNo number,CustName varchar2(15),AccountType varchar2(15)


default 'Current',Balance Number,passportno varchar2(15) ,CONSTRAINT Pkey Primary
key(AccountNo),CONSTRAINT ChkBal check(Balance>5000),CONSTRAINT UU unique (passportno));

11. See the contents of “User_Constraints” table and note the constraint number and type

of constraints for your above table.

12. Drop the Primary Key & Unique Constraint from Account4 Table.

Ans: alter table account4 drop constraint uu;

alter table account4 drop constraint PKEY;


Saurabh Tariyal Sec:J 1918677

13. WAQ (write a ans) to create an “Account5” table without any constraints:

AccountNo Number (No constraint)

CustName Varchar2 (No constraint)

Balance Number (No constraint)

Ans: create table account5(accno number,custname varchar2(15),balance number);

14. Insert the following data in the above table:

8111 Puneet 1000

8111 Mudit 2000


8114 Kumar 3000

8115 Shweta 500


8115 Deepak 6000

:
Saurabh Tariyal Sec:J 1918677

15. See the structure of Account5 table.

16. Add the primary key constraint for AccountNo in Account5 Table

(If an error comes, note the error and write the reason)

17. In case of error in above ques, rectify the above table contents and repeat above Question

(Write all the steps and queries)

 update account5 set accno=8116 where custname='sweta'


 update account5 set accno=8110 where custname='mudit'
Saurabh Tariyal Sec:J 1918677

18. See the contents of “User_Constraints” table and search the name of Account5 table in it.

19. Add UNIQUE constraint in Account5 Table for CustName field.

Ans: alter table account5 add constraint cname Unique (custname);

20. Add Check constraint in Account5 Table for Balance field. ( Amount > 1500)

Ans: alter table account5 add constraint chec check(balance>1500);


Saurabh Tariyal Sec:J 1918677

21. In case of error in above ques, rectify the above table contents and repeat above Question

(Write all the steps and queries)


Saurabh Tariyal Sec:J 1918677

Assignment 4
PART – A

SQL> select upper('gehu') from dual;

SQL> select lower('GEHU') from dual;


Output:

SQL> select initcap(‘hello world') from dual;

SQL> select ltrim(' hai') from dual;


Saurabh Tariyal Sec:J 1918677

SQL> select rtrim('hai ') from dual;


SQL> select concat('GEHU',' university') from dual;
SQL> select length('GEHU’) from dual;
SQL> select replace('GEHU university', 'GEHU','GEU') from dual;

SQL> select substr('UNIVERSITY', 4,6) from dual;

SQL> select rpad('GEHU',10,'*')from dual;


Saurabh Tariyal Sec:J 1918677

SQL> select lpad('GEHU',10,'*') from dual;

SQL> select Instr('COCOON',’O’) from dual;

SQL> select replace('Dany','y','ie') from dual;

SQL> select translate('cold','ld','ol') from dual;


Saurabh Tariyal Sec:J 1918677

SQL> select sysdate from dual;

SQL> select round(sysdate)from dual;

SQL> select add_months(sysdate,3)from dual;

SQL> select last_day(sysdate)from dual;

SQL> select sysdate+20 from dual;

SQL> select next_day(sysdate,'tuesday')from dual;


Saurabh Tariyal Sec:J 1918677

NUMERIC FUNCTIONS:
SQL> select round(15.6789)from dual;

SQL> select ceil(23.20)from dual;

SQL> select floor(34.56)from dual;

SQL> select trunc(15.56743)from dual;

SQL> select sign(-345)from dual;

SQL> select abs(-70)from dual;


Saurabh Tariyal Sec:J 1918677

MATH FUNCTIONS:
SQL> select abs(45) from dual;

SQL> select power(10,12) from dual;

SQL> select mod(11,5) from dual;

SQL> select exp(10) from dual;

SQL> select sqrt(225) from dual;


Saurabh Tariyal Sec:J 1918677

Practical : PART – B

Q1. What are Single Row and Multiple Row Functions in Oracle SQL. Explain in detail.
Ans.
Single Row functions - Single row functions are the one who work on single row and return
one output per row. For example, length and case conversion functions are single row
functions.
Single row functions can be character functions, numeric functions, date functions, and
conversion functions. Single-row functions include TO_CHAR, TO_DATE, UPPER,
LOWER etcSingle-row functions can be used also in the SET clause of and UPDATE
command. Single-row functions cannot be used in a HAVING clause.

Multiple Row functions - Multiple row functions work upon group of rows and return one
result for the complete set of rows. They are also known as Group Functions. This is a
powerful feature because it allows you to generate subtotals, sums and averages within the
SQL that is retrieving the data.  For now, we will apply the functions to all the rows we
return.  In the next chapter, we will break up our returning rows into groups and apply the
functions to each of the groups independently.

Q2. Prepare a detailed list and notes on Following Type of Functions:


 Character Functions- Character functions operate on values of dataype 
CHAR or VARCHAR.

LOWER- Returns a given string in lower case.

UPPER- Returns a given string in UPPER case.

INITCAP- Returns a given string with Initial letter in capital.

LENGTH- Returns the length of a given string.

SUBSTR- Returns a substring from a given string. Starting from position p to n


characters.

INSTR- Tests whether a given character occurs in the given string or not. If the character
occurs in the string then returns the first position of its occurrence otherwise returns 0.

REPLACE- Replaces a given set of characters in a string with another set of characters.

INSTR- Tests whether a given character occurs in the given string or not. If the character
occurs in the string then returns the first position of its occurrence otherwise returns 0.
Saurabh Tariyal Sec:J 1918677

 Number Functions –
Addition-- The addition operator (+).
Subtraction- The subtraction operator (-).
Multiplication- The multiplication operator (*).
Division- The division operator (/).
ABS- Returns the absolute value of n.If n is 0 or a positive integer, returns n.
CEIL- Returns the smallest integer value not less than n.
EXP- Exponentiation, where the base is e.Returns the value of e (the base of natural
logarithms) raised to the power n.
LOG- Logarithm. log(n, m) takes two arguments, where n is the base, and m is the
value you are taking the logarithm of.
MOD- Modulo. Returns the remainder of n divided by m.
ROUND- Returns a number rounded to the specified decimal place.
The unary (one argument) version takes only one argument (the number to be
rounded) and drops the decimal (non-integral) portion of the input.

SQRT- Returns the nonnegative square root of n.


POWER- Returns the value (as a double) of n raised to the power of m.
SIN- The sine of n, where the angle of n is in radians.
COS- The cosine of n, where the angle of n is in radians.

 Date Functions-
NOW(): Returns the current date and time.
CURDATE(): Returns the current date.
CURTIME(): Returns the current time.
DATE(): Extracts the date part of a date or date/time expression.
EXTRACT(): Returns a single part of a date/time.
DATE_ADD() : Adds a specified time interval to a date
DATE_SUB(): Subtracts a specified time interval from a date. Syntax for
DATE_SUB is same as DATE_ADD just the difference is that DATE_SUB is used to
subtract a given interval of date.
DATEDIFF(): Returns the number of days between two dates.
DATE_FORMAT(): Displays date/time data in different formats
Saurabh Tariyal Sec:J 1918677

 Conversions Functions-
TO_CHAR function-used to typecast a numeric or date input to character type with a
format model
TO_NUMBER function-converts a character value to a numeric datatype. If the string
being converted contains nonnumeric characters, the function returns an error.
TO_DATE function-The function takes character values as input and returns
formatted date equivalent of the same. it allows users to enter a date in any format,
and then it converts the entry into the default format used by Oracle 11g.

 NVL : In SQL, NVL() converts a null value to an actual value. Data types that can be
used are date, character and number. Data type must match with each other i.e.
expr1 and expr2 must of same data type.
Syntax –
NVL (expr1, expr2)
expr1 is the source value or expression that may contain a null.
expr2 is the target value for converting the null.

e.g- SELECT salary, NVL(commission_pct, 0),(salary*12)+


(salary*12*NVL(commission_pct, 0)) annual_salary FROM employees;

 NVL2(expr1, expr2, expr3) : The NVL2 function examines the first expression. If the
first expression is not null, then the NVL2 function returns the second expression. If
the first expression is null, then the third expression is returned i.e. If expr1 is not null,
NVL2 returns expr2. If expr1 is null, NVL2 returns expr3.
Syntax –
NVL2 (expr1, expr2, expr3)
expr1 is the source value or expression that may contain null
expr2 is the value returned if expr1 is not null
expr3 is the value returned if expr1 is null
Saurabh Tariyal Sec:J 1918677

e.g- SELECT last_name, salary, commission_pct,


NVL2(commission_pct, ’SAL+COMM’, ’SAL’)
income FROM employees;

 CASE- The CASE statement goes through conditions and returns a value when the
first condition is met (like an if-then-else statement). So, once a condition is true, it
will stop reading and return the result.
CASE Syntax- CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
e.g- SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;

 DECODE -Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-


ELSE statement.The DECODE function decodes an expression in a way similar to
the IF-THEN-ELSE logic used in various languages.
e.g- DECODE(col|expression, search1, result1
[, search2, result2,...,][, default])
e.g- SELECT product_id,
DECODE (warehouse_id, 1, 'Southlake',
2, 'San Francisco',
3, 'New Jersey',
4, 'Seattle',
'Non domestic')
"Location of inventory" FROM inventories
WHERE product_id < 1775;
Saurabh Tariyal Sec:J 1918677

Assignment 6
1.Write a SQL query to find the maximum order (purchase) amount for each customer. The
customer ID should be in the range 3002 and 3007(Begin and end values are included.). Filter
the rows for maximum order (purchase) amount is higher than 1000. Return customer id and
maximum purchase amount.
Ans.
SELECT customer_id,MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007
GROUP BY customer_id;
Output:

2.Write a SQL query to find the maximum order (purchase) amount by the combination of each
customer and order date. Filter the rows for maximum order (purchase) amount is either 2000,
3000, 5760, 6000. Return customer id, order date and maximum purchase amount.
Ans.

SELECT customer_id,MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007
GROUP BY customer_id
HAVING MAX(purch_amt)>1000;

3. Write a SQL query to find the maximum order (purchase) amount in the range 2000, 6000
(Begin and end values are included.) by combination of each customer and order date. Return
customer id, order date and maximum purchase amount.
Ans.

SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
Saurabh Tariyal Sec:J 1918677

GROUP BY customer_id,ord_date
HAVING MAX(purch_amt) BETWEEN 2000 AND 6000;

4. Write a SQL query to find highest order (purchase) amount by each customer in a particular
order date. Filter the result by highest order (purchase) amount above 2000.00. Return
customer id, order date and maximum purchase amount.
Ans.

SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt)>2000.00;
Saurabh Tariyal Sec:J 1918677

5. Write a SQL query to find the highest purchase amount on '2012-08-17' by each salesperson.
Return salesperson ID, purchase amount.

Ans.

SELECT salesman_id,MAX(purch_amt)
FROM orders
WHERE ord_date = '2012-08-17'
GROUP BY salesman_id;

Join
1. Write a SQL query to find the salesperson and customer who belongs to same city.
Return Salesman, cust_name and city.

Ans.
SELECT salesman.name AS "Salesman",
customer.cust_name, customer.city
FROM salesman,customer
WHERE salesman.city=customer.city;
Saurabh Tariyal Sec:J 1918677

2. Write a SQL query to find the salesperson(s) and the customer(s) he handle. Return
Customer Name, city, Salesman, commission.

Ans.
SELECT a.cust_name AS "Customer Name",
a.city, b.name AS "Salesman", b.commission
FROM customer a
INNER JOIN salesman b
ON a.salesman_id=b.salesman_id;

3. Write a SQL query to find those salespersons do not live in the same city where their
customers live and received a commission from the company more than 12%. Return
Customer Name, customer city, Salesman, salesman city, commission.

Ans.
SELECT a.cust_name AS "Customer Name",
a.city, b.name AS "Salesman", b.commission
FROM customer a
INNER JOIN salesman b
ON a.salesman_id=b.salesman_id
WHERE b.commission>.12;
Saurabh Tariyal Sec:J 1918677

4.Write a SQL query to display the cust_name, customer city, grade, Salesman, salesman city.
The result should be ordered by ascending on customer_id.

SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",b.city
FROM customer a
LEFT JOIN salesman b
ON a.salesman_id=b.salesman_id
order by a.customer_id;

5.Write a SQL statement to make a list in ascending order for the salesmen who works
either for one or more customer or not yet join under any of the customers.
Ans.

SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman", b.city
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
ORDER BY b.salesman_id;

You might also like