You are on page 1of 25

1) Type the following select statement at SQL prompt and examine the output.

Select last name, job id, salary as sal from employees;

SQL> SELECT LAST_NAME, JOB_ID, SALARY SAL


FROM EMPLOYEES;

2) Type the following select statement at SQL prompt and examine the output.
Select * from salgrade;

SQL> SELECT * FROM SALGRADE;

3) There are four coding errors in this statement. Can you identify them?
Select employee id, last name sal x 12 annual salary from employees;

SQL> SELECT EMPLOYEE_ID, LAST_NAME, SALARY * 12 “Annual Salary”


FROM EMPLOYEES;

4) Display the structure of the departments table.

SQL> DESC DEPT

5) Display the last name concatenated with the job ID, separated by a comma and space,
and name the column Employee and Title.

SQL> SELECT LAST_NAME || ‘, ‘ || JOB_ID “Employee and Title”


FROM EMPLOYEES;

6) Display department numbers and names from department table.

SQL> SELECT DEPTNO, DNAME FROM DEPT;

7) Retrieve the list of employees’ names, employee ID, job and salary from EMP table.

SQL> SELECT EMPNO, ENAME, JOB, SAL FROM EMP;

8) Retrieve the list of names, jobs with “Designation” heading, salary with “Basic Salary”
heading from EMP table.

SQL> SELECT ENAME, JOB DESIGNATION, SAL "Basic Salary" FROM EMP;
9) List out the names of all Tables.

SQL> SELECT * FROM TAB;

10) Display employee name, job, 8% on sal as bonus , 9.75% on salary as DA.

SQL> SELECT ENAME, JOB, SAL * 0.08 BONUS, SAL * .975 AS DA


FROM EMP;

11) Display employee name, job, hiredate, salary, 9.25% on salary as Income Tax and
Net Salary (salary – Tax).

SQL> SELECT ENAME, JOB, HIREDATE, SAL, SAL * 9.25 / 100 "Income Tax",
SAL - (SAL * 9.25 / 100) "Net Salary" FROM EMP;

12) Display employee name, salary, 8% on salary as bonus and 2% as Tax on (salary +
bonus).

SQL> SELECT ENAME, SAL, SAL * 8 / 100 BONUS, (SAL + SAL * 8 / 100) * 2 /
100 "Tax" FROM EMP;

13) Display the following formats


a) {ename} working as a {job} with basic salary {sal}

SQL> SELECT ENAME || ' working as a ' || JOB || ' with basic ' || SAL AS
"EMPLOYEE INFORMATION" FROM EMP;

b) {ename} working in {deptno} department as a {job} with basic {sal}

SQL> SELECT ENAME || ' working in ' || DEPTNO || ' department as a ' || JOB || '
with basic ' || SAL EMP_INFO FROM EMP;

c) The employee {ename}({job}) paying Income Tax {sal*9.75/100}

SQL> SELECT ENAME || '(' || JOB || ')' || ' paying Income Tax ' || SAL * 9.75 / 100
TAX_INFO FROM EMP;

14) Produce the following output.


EMPLOYEE
----------------
SMITH(Clerk)
ALLEN(Salesman)
WARD(Salesman)
JONES(Manager)
MARTIN(Salesman) and so on………………

SQL> SELECT ENAME || '(' || JOB || ')' FROM EMP;

15) Display all employees who were hired during 1983.

SQL> SELECT * FROM EMP WHERE HIREDATE >= '01-JAN-1983' AND


HIREDATE <= '31-DEC-1983';

16) Display details who are working in the departments 10, 20 as a MANAGER.

SQL> SELECT * FROM EMP WHERE JOB='MANAGER' AND (DEPTNO = 10


OR DEPTNO = 20);

17) Develop a query that will accept a given job name. Execute the query a number of
times to test.

SQL> SELECT * FROM EMP WHERE JOB = ‘&JOB’;

18) Display the name, job, salary and bonus (8 % of salary) with “BONUS” heading and
output should be in the order of bonus.

SQL> SELECT ENAME, JOB, SAL, SAL * 8 / 100 BONUS FROM EMP ORDER
BY BONUS;

19) Display employee’s details, who are not getting commission. Write queries in
possible methods.

SQL> SELECT * FROM EMPLOYEES WHERE COMMISSION_PCT = 0 OR


COMMISSION_PCT IS NULL;

20) Display details whose employee name contain ‘E’ as a second character from end of
the string.

SQL> SELECT * FROM EMP WHERE SUBSTR (ENAME,-2, 1) = 'E';

SQL> SELECT * FROM EMP WHERE ENAME LIKE '%E_';


21) Display details whose employee name contains “S” or “s”.

SQL> SELECT * FROM EMP WHERE UPPER (ENAME) LIKE '%S%';

22) Display employee name, job, salary, commission and total salary (sal + comm) with
ordering highest total salary first.

SQL> SELECT ENAME, JOB, COMM, (SAL + COMM) "TOTAL SALARY"


FROM EMP ORDER BY "TOTAL SALARY";

23) Find the most recently hired employees in each department.

SQL> SELECT * FROM EMP WHERE (HIREDATE, DEPTNO) IN (SELECT


MAX (HIREDATE), DEPTNO FROM EMP GROUP BY DEPTNO)

24) Show all data of the clerks who have been hired after the year 1997.

SQL> SELECT * FROM EMP WHERE UPPER (JOB) = 'CLERK' AND


HIREDATE > '31-DEC-1997';

25) Show the last name, job, salary, and commission of those employees who earn
commission. Sort the data by the salary in descending order.

SQL> SELECT LAST_NAME, JOB_ID, SALARY, COMMISSION_PCT


FROM EMPLOYEES WHERE COMMISSION_PCT != 0 OR
COMMISSION_PCT IS NOT NULL;

26) Show the employees that have no commission with a 10% raise in their salary (round
off the salaries).

SQL> SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, ROUND (SAL + SAL
* 10 / 100), COMM, DEPTNO
FROM EMP
WHERE COMM = 0 OR COMM IS NULL;

27) Show those employees that have a name starting with J, K, L, or M.

SQL> SELECT * FROM EMP WHERE SUBSTR (ENAME, 1, 1) IN


('J','K','L','M');
SQL Assignments

Day 2

1) Replace ‘CLERKS’ as ‘MANAGER’, ‘MANAGER’ as ‘BOSS’ and rest undefined.

SQL> SELECT ENAME, DECODE (RTRIM (UPPER (JOB)), 'CLERK',


'MANAGER', 'MANAGER', 'BOSS', JOB) JOB
FROM EMP

2) Find the average salary for each job. Remember that salesman getting commission.

SQL> SELECT JOB, AVG (SAL+NVL (COMM, 0)) FROM EMP GROUP BY
JOB;

3) Display employee name, job, salary, department name and date of join. Sort latest
joined first for every department.

SQL> SELECT ENAME, JOB, SAL, DNAME, HIREDATE


FROM EMP, DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO
ORDER BY EMP.DEPTNO, HIREDATE DESC

4) Display minimum, maximum and average salary for each department.

SQL> SELECT DEPTNO, MIN (SAL) MINIMUM, MAX (SAL) MAXIMUM,


AVG (SAL) AVERAGE
FROM EMP
GROUP BY DEPTNO;

5) List the ename, job, annual salary, department number, department name and grade for
employees who earn more than 36000 a year and who are clerks.

SQL> SELECT E.ENAME, E.JOB, E.SAL * 12 "Annual Salary", D.DEPTNO,


D.DNAME, S.GRADE
FROM EMP E, DEPT D, SALGRADE S
WHERE E.DEPTNO = D.DEPTNO AND
(E.SAL * 12) > 12000 AND
E.JOB = 'CLERK' AND
E.SAL BETWEEN S.LOSAL AND S.HISAL

6) Count number of employee working in Chicago location.

SQL> SELECT COUNT (*) FROM EMP


WHERE DEPTNO = (SELECT DEPTNO
FROM DEPT
WHERE UPPER (RTRIM (LOC)) = ‘CHICAGO’)

SQL> SELECT COUNT (*) FROM EMP, DEPT


WHERE EMP.DEPTNO = DEPT.DEPTNO AND
UPPER (RTRIM (LOC)) = ‘CHICAGO’;

7) Find out employee details whose salary is less than average salary of New York.

SQL> SELECT E.*


FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO AND
E.SAL < (SELECT AVG (SAL)
FROM EMP, DEPT WHERE EMP.DEPTNO = DEPT.DEPTNOAND
UPPER (RTRIM (LOC)) = 'NEW YORK'))

8) Display employee name, job, sal, comm and total salary (sal+comm) whose total
salary more than maximum total salary of SALES department.

SQL> SELECT ENAME, JOB, SAL, NVL (COMM, 0), SAL + NVL (COMM, 0)
“Total Salary”
FROM EMP
WHERE (SAL + NVL (COMM, 0)) > (SELECT MAX (SAL)
FROM EMP
WHERE DEPTNO = (SELECT DEPTNO
FROM DEPT
WHERE DNAME = ‘SALES’));

SQL> SELECT ENAME, JOB, SAL, NVL (COMM, 0), SAL + NVL (COMM, 0)
“Total Salary”
FROM EMP
WHERE (SAL + NVL (COMM, 0)) > (SELECT MAX (EMP.SAL)
FROM EMP, DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO AND
DEPT.DNAME = ‘SALES’));

9) Display department name and location in which department not have employees. Write
this query with SET operators.

SQL> SELECT DNAME, LOC FROM DEPT


MINUS
SELECT DNAME, LOC FROM DEPT , EMP
WHERE DEPT.DEPTNO =EMP.DEPTNO ;

The following queries by using Employees, Departments, Locations, etc.


10) Show the department names, locations, names, job titles, and salaries of employees
who work in location 1800.
SQL> SELECT DEPT.DEPARTMENT_NAME, LOC.CITY, EMP.FIRST_NAME
|| ‘ ‘ || EMP.LAST_NAME NAME, JOBS.JOB_TITLE, EMP.SALARY
FROM EMPLOYEES EMP, DEPARTMENTS DEPT, LOCATIONS LOC, JOBS
WHERE EMP.DEPARTMENT_ID = DEPT.DEPARTMENT_ID AND
EMP.JOB_ID = JOBS.JOB_ID AND
DEPT.LOCATION_ID = LOC.LOCATION_ID AND
DEPT.LOCATION_ID = 1800;

11) How many employees have a name that ends with an n? Create two possible
solutions.

SELECT COUNT (*)


FROM EMPLOYEES
WHERE UPPER (SUBSTR (TRIM (FIRST_NAME), -1)) = ‘N’ OR
UPPER (SUBSTR (TRIM (LAST_NAME), -1)) = ‘N’;

SELECT COUNT (*)


FROM EMPLOYEES
WHERE UPPER (TRIM (FIRST_NAME)) LIKE ‘%N’ OR
UPPER (TRIM (LAST_NAME)) LIKE ‘%N’

SELECT ‘FIRST NAME’,COUNT (*)


FROM EMPLOYEES
WHERE UPPER (SUBSTR (TRIM (FIRST_NAME), -1)) = ‘N’
UNION
SELECT ‘LAST NAME’,COUNT (*)
FROM EMPLOYEES
WHERE UPPER (SUBSTR (TRIM (LAST_NAME), -1)) = ‘N’;

12) Which jobs are found in departments 10 and 20?

SELECT JOBS.JOB_TITLE
FROM EMPLOYEES EMP, JOBS
WHERE EMP.JOB_ID = JOBS.JOB_ID AND
EMP.DEPARTMENT_ID IN (10, 20);

13) Show all employees who were hired in the first half of the month (before the 16th of
the month)

SQL> SELECT * FROM EMPLOYEES WHERE TO_CHAR (HIRE_DATE,’DD’)


< 16;

14) Find the job that was filled in the first half of 1990 and the same job that was filled
during in 1991.
SQL> SELECT JOB_TITLE
FROM JOBS
WHERE JOB_ID IN (SELECT JOB_ID
FROM EMPLOYEES
WHERE TO_NUMBER (TO_CHAR (HIRE_DATE, ’MMYYYY’))
BETWEEN 011990 AND 061990)
INTERSECT
SELECT JOB_TITLE
FROM JOBS
WHERE JOB_ID IN (SELECT JOB_ID
FROM EMPLOYEES
WHERE TO_CHAR (HIRE_DATE, ‘YYYY’) = 1991);

15) Show the department number, department name, and the number of employees
working in each department ordered by count.

SQL> SELECT EMP.DEPARTMENT_ID, DEPT.DEPARTMENT_NAME,


COUNT (*)
FROM EMPLOYEES EMP, DEPARTMENTS DEPT
WHERE EMP.DEPARTMENT_ID = DEPT.DEPARTMENT_ID
GROUP BY EMP.DEPARTMENT_ID, DEPT.DEPARTMENT_NAME
ORDER BY 3 DESC;
SQL Assignments

Day 3

1) Update the salaries by adding their commission to salary in EMP table;

SQL> UPDATE EMP SET SAL = NVL (SAL, 0) + NVL (COMM, 0);

2) Update commission as 200 for department number 20

SQL> UPDATE EMP


SET COMM = 200
WHERE DEPTNO = 20 AND
NVL (COMM,0) = 0;

3) Update salary by 1000 of employees who are working in accounting department and
whose job experience is more than 225 months.

SQL> UPDATE EMP


SET SAL = NVL (SAL, 0) + 1000
WHERE DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE UPPER (TRIM (DNAME)) = 'ACCOUNTING')
AND MONTHS_BETWEEN (SYSDATE, HIREDATE) > 225

4) Delete the rows from EMP table who are working as a clerk in the department SALES.

SQL> DELETE FROM EMP


WHERE UPPER (TRIM (JOB)) ='CLERK' AND
DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE UPPER (TRIM (DNAME)) = 'SALES')

5) Create the following tables with appropriate constraints and insert the following data.

SALESPEOPLE:
SNUM SNAME CITY COMM
1001 PEEL LONDON 0.12
1002 SSERRES SAN JOSE 0.13
1003 MOTIKA LONDON 0.11
1004 AXELROD NEW YORK 0.10

CUSTOMERS:
CNUM CNAME CITY RATING SNUM
2001 HOFFMAN LONDON 100 1001
2002 GIOVANNI ROME 200 1003
2003 LIU SAN JOSE 200 1002
2004 GRASS SAN JOSE 300 1002

ORDERS:
ONUM AMT ODATE CNUM
3001 18.69 10/03/90 2001
3003 767.19 12/08/90 2001
3002 1900.10 15/01/91 2004
3005 5150.45 03/10/90 2003
3006 1098.16 02/03/92 2002

SQL> CREATE TABLE SALESPEOPLE


(SNUM NUMBER (4) CONSTRAINT PK_SNUM PRIMARY KEY,
SNAME VARCHAR2 (10),
CITY VARCHAR2 (10),
COMM NUMBER (2, 2));

INSERT INTO SALESPEOPLE VALUES


(&SNUM,
UPPER ('&SNAME'),
UPPER ('&CITY'),
&COMM);

INSERT INTO SALESPEOPLE VALUES


(1001,
UPPER ('PEEL'),
UPPER ('LONDON'),
0.12);

INSERT INTO SALESPEOPLE VALUES


(1002,
UPPER ('SSERRES'),
UPPER ('SAN JOSE'),
0.13);

INSERT INTO SALESPEOPLE VALUES


(1003,
UPPER ('MOTIKA'),
UPPER ('LONDON'),
0.11)

INSERT INTO SALESPEOPLE VALUES


(1004,
UPPER ('AXELROD'),
UPPER ('NEW YORK'),
0.10)

CREATE TABLE CUSTOMERS


(CNUM NUMBER (4) CONSTRAINT PK_CNUM PRIMARY KEY,
CNAME VARCHAR2 (10),
CITY VARCHAR2 (10),
RATING NUMBER (3),
SNUM NUMBER (4) CONSTRAINT FK_SNUM REFERENCES
SALESPEOPLE (SNUM));

INSERT INTO CUSTOMERS VALUES


(&CNUM,
UPPER ('&CNAME'),
UPPER ('&CITY'),
&RATING,
&SNUM);

INSERT INTO CUSTOMERS VALUES


(2001,
UPPER ('HOFFMAN'),
UPPER ('LONDON'),
100,
1001)

INSERT INTO CUSTOMERS VALUES


(2002,
UPPER ('GIOVANNI'),
UPPER ('ROME'),
200,
1003)

INSERT INTO CUSTOMERS VALUES


(2003,
UPPER ('LIU'),
UPPER ('SAN JOSE'),
200,
1002)

INSERT INTO CUSTOMERS VALUES


(2004,
UPPER ('GRASS'),
UPPER ('SAN JOSE'),
300,
1002)
CREATE TABLE ORDERS
(ONUM NUMBER (4) CONSTRAINT PK_ONUM PRIMARY KEY,
AMT NUMBER (7, 2),
ODATE DATE,
CNUM NUMBER (4) CONSTRAINT FK_CNUM REFERENCES
CUSTOMERS);

INSERT INTO ORDERS VALUES


(&ONUM,
&AMT,
‘&ODATE’,
&CNUM);

INSERT INTO ORDERS VALUES


(3001,
18.69,
'10-DEC-1990',
2001)

INSERT INTO ORDERS VALUES


(3002,
1900.10,
'15-JAN-1991',
2004)

INSERT INTO ORDERS VALUES


(3003,
767.19,
'12-AUG-1990',
2001)

INSERT INTO ORDERS VALUES


(3005,
5150.45,
'03-OCT-1990',
2003)

INSERT INTO ORDERS VALUES


(3006,
1098.16,
'02-MAR-1992',
2002)

6) Find the largest order taken by each salesperson.


SQL> SELECT S.SNUM SALESPERSON, MAX (O.AMT) “ORDER AMOUNT”
FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM AND
(C.CNUM, O.AMT) IN (SELECT O.CNUM, MAX (O.AMT)
FROM ORDERS O
GROUP BY O.CNUM)
GROUP BY S.SNUM
UNION
SELECT S.SNUM, 0
FROM SALESPEOPLE S
WHERE NOT EXISTS (SELECT * FROM CUSTOMERS C
WHERE S.SNUM = C.SNUM);

7) Find which salespeople currently have orders in the Orders table.

SQL> SELECT DISTINCT S.SNUM, S.SNAME


FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM;

8) List names of all customers matched with the salespeople serving them.

SQL> SELECT DISTINCT C.CNUM, C.CNAME, S.SNUM, S.SNAME


FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM;

9) Count the orders of each of the salespeople and output the result descending order.

SQL> SELECT S.SNUM, COUNT (*) “No. of Orders placed”


FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM
GROUP BY S.SNUM
UNION
SELECT S.SNUM, 0
FROM SALESPEOPLE S
WHERE NOT EXISTS (SELECT * FROM CUSTOMERS C
WHERE S.SNUM = C.SNUM)
ORDER BY 2 DESC;
10) List the customer table if and only if one or more of the customer located in San Jose.

SQL> SELECT * FROM CUSTOMERS WHERE (SELECT COUNT (*) FROM


CUSTOMERS WHERE UPPER (TRIM (CITY)) = ‘SAN JOSE’) > 1;

11) Display customer name, order amount, year for largest order amount taken in each
year.

SQL> SELECT C.CNAME, O.AMT, TO_CHAR (O.ODATE,’YYYY’) “Year”


FROM CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
(TO_CHAR (O.ODATE,’YYYY’), O.AMT) IN
(SELECT TO_CHAR (ODATE,'YYYY'), MAX (AMT)
FROM ORDERS
GROUP BY TO_CHAR (ODATE,'YYYY'));

12) Find all the customers with orders on October 3.

SQL> SELECT *
FROM CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
O.ODATE LIKE ’03-OCT%’;
SQL Assignments

Day 5

1) Update the salaries by adding their commission to salary in EMP table;

SQL> UPDATE EMP SET SAL = NVL (SAL, 0) + NVL (COMM, 0);

2) Update commission as 200 for department number 20

SQL> UPDATE EMP


SET COMM = 200
WHERE DEPTNO = 20 AND
NVL (COMM,0) = 0;

3) Update salary by 1000 of employees who are working in accounting department and
whose job experience is more than 225 months.

SQL> UPDATE EMP


SET SAL = NVL (SAL, 0) + 1000
WHERE DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE UPPER (TRIM (DNAME)) = 'ACCOUNTING')
AND MONTHS_BETWEEN (SYSDATE, HIREDATE) > 225

4) Delete the rows from EMP table who are working as a clerk in the department SALES.

SQL> DELETE FROM EMP


WHERE UPPER (TRIM (JOB)) ='CLERK' AND
DEPTNO IN (SELECT DEPTNO
FROM DEPT
WHERE UPPER (TRIM (DNAME)) = 'SALES')

5) Create the following tables with appropriate constraints and insert the following data.

SALESPEOPLE:
SNUM SNAME CITY COMM
1001 PEEL LONDON 0.12
1002 SSERRES SAN JOSE 0.13
1003 MOTIKA LONDON 0.11
1004 AXELROD NEW YORK 0.10

CUSTOMERS:
CNUM CNAME CITY RATING SNUM
2001 HOFFMAN LONDON 100 1001
2002 GIOVANNI ROME 200 1003
2003 LIU SAN JOSE 200 1002
2004 GRASS SAN JOSE 300 1002

ORDERS:
ONUM AMT ODATE CNUM
3001 18.69 10/03/90 2001
3003 767.19 12/08/90 2001
3002 1900.10 15/01/91 2004
3005 5150.45 03/10/90 2003
3006 1098.16 02/03/92 2002

SQL> CREATE TABLE SALESPEOPLE


(SNUM NUMBER (4) CONSTRAINT PK_SNUM PRIMARY KEY,
SNAME VARCHAR2 (10),
CITY VARCHAR2 (10),
COMM NUMBER (2, 2));

INSERT INTO SALESPEOPLE VALUES


(&SNUM,
UPPER ('&SNAME'),
UPPER ('&CITY'),
&COMM);

INSERT INTO SALESPEOPLE VALUES


(1001,
UPPER ('PEEL'),
UPPER ('LONDON'),
0.12);

INSERT INTO SALESPEOPLE VALUES


(1002,
UPPER ('SSERRES'),
UPPER ('SAN JOSE'),
0.13);

INSERT INTO SALESPEOPLE VALUES


(1003,
UPPER ('MOTIKA'),
UPPER ('LONDON'),
0.11)

INSERT INTO SALESPEOPLE VALUES


(1004,
UPPER ('AXELROD'),
UPPER ('NEW YORK'),
0.10)

CREATE TABLE CUSTOMERS


(CNUM NUMBER (4) CONSTRAINT PK_CNUM PRIMARY KEY,
CNAME VARCHAR2 (10),
CITY VARCHAR2 (10),
RATING NUMBER (3),
SNUM NUMBER (4) CONSTRAINT FK_SNUM REFERENCES
SALESPEOPLE (SNUM));

INSERT INTO CUSTOMERS VALUES


(&CNUM,
UPPER ('&CNAME'),
UPPER ('&CITY'),
&RATING,
&SNUM);

INSERT INTO CUSTOMERS VALUES


(2001,
UPPER ('HOFFMAN'),
UPPER ('LONDON'),
100,
1001)

INSERT INTO CUSTOMERS VALUES


(2002,
UPPER ('GIOVANNI'),
UPPER ('ROME'),
200,
1003)

INSERT INTO CUSTOMERS VALUES


(2003,
UPPER ('LIU'),
UPPER ('SAN JOSE'),
200,
1002)

INSERT INTO CUSTOMERS VALUES


(2004,
UPPER ('GRASS'),
UPPER ('SAN JOSE'),
300,
1002)
CREATE TABLE ORDERS
(ONUM NUMBER (4) CONSTRAINT PK_ONUM PRIMARY KEY,
AMT NUMBER (7, 2),
ODATE DATE,
CNUM NUMBER (4) CONSTRAINT FK_CNUM REFERENCES
CUSTOMERS);

INSERT INTO ORDERS VALUES


(&ONUM,
&AMT,
‘&ODATE’,
&CNUM);

INSERT INTO ORDERS VALUES


(3001,
18.69,
'10-DEC-1990',
2001)

INSERT INTO ORDERS VALUES


(3002,
1900.10,
'15-JAN-1991',
2004)

INSERT INTO ORDERS VALUES


(3003,
767.19,
'12-AUG-1990',
2001)

INSERT INTO ORDERS VALUES


(3005,
5150.45,
'03-OCT-1990',
2003)

INSERT INTO ORDERS VALUES


(3006,
1098.16,
'02-MAR-1992',
2002)

6) Find the largest order taken by each salesperson.


SQL> SELECT S.SNUM SALESPERSON, MAX (O.AMT) “ORDER AMOUNT”
FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM AND
(C.CNUM, O.AMT) IN (SELECT O.CNUM, MAX (O.AMT)
FROM ORDERS O
GROUP BY O.CNUM)
GROUP BY S.SNUM
UNION
SELECT S.SNUM, 0
FROM SALESPEOPLE S
WHERE NOT EXISTS (SELECT * FROM CUSTOMERS C
WHERE S.SNUM = C.SNUM);

7) Find which salespeople currently have orders in the Orders table.

SQL> SELECT DISTINCT S.SNUM, S.SNAME


FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM;

8) List names of all customers matched with the salespeople serving them.

SQL> SELECT DISTINCT C.CNUM, C.CNAME, S.SNUM, S.SNAME


FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM;

9) Count the orders of each of the salespeople and output the result descending order.

SQL> SELECT S.SNUM, COUNT (*) “No. of Orders placed”


FROM SALESPEOPLE S, CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
C.SNUM = S.SNUM
GROUP BY S.SNUM
UNION
SELECT S.SNUM, 0
FROM SALESPEOPLE S
WHERE NOT EXISTS (SELECT * FROM CUSTOMERS C
WHERE S.SNUM = C.SNUM)
ORDER BY 2 DESC;
10) List the customer table if and only if one or more of the customer located in San Jose.

SQL> SELECT * FROM CUSTOMERS WHERE (SELECT COUNT (*) FROM


CUSTOMERS WHERE UPPER (TRIM (CITY)) = ‘SAN JOSE’) > 1;

11) Display customer name, order amount, year for largest order amount taken in each
year.

SQL> SELECT C.CNAME, O.AMT, TO_CHAR (O.ODATE,’YYYY’) “Year”


FROM CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
(TO_CHAR (O.ODATE,’YYYY’), O.AMT) IN
(SELECT TO_CHAR (ODATE,'YYYY'), MAX (AMT)
FROM ORDERS
GROUP BY TO_CHAR (ODATE,'YYYY'));

12) Find all the customers with orders on October 3.

SQL> SELECT *
FROM CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
O.ODATE LIKE ’03-OCT%’;

10. Write a program to generate even numbers from 1 to 50 horizontally.

DECLARE
n NUMBER(2):=0;
c VARCHAR2(100):=’’;
BEGIN
WHILE N < 51
LOOP
CONCAT (C||’,’, TO_CHAR(N));
END LOOP;
dbms_output.put_line (susstr(c,1,length(c) – 1)
END;
SQL Assignments

DAY3

1) Update the salaries by adding their commission to them in EMP table.

2) Update employee commission in EMP table by giving 200 who are not getting
commission in 20th department.

3) Update salaries in EMP table by adding 1000 who are working in ACCOUNTING
department and job experience is more then 225 months.

4) Delete the rows from EMP table who are working as a clerk in the department SALES.

5) Create the following tables with appropriate constraints and insert the following data.

SALESPEOPLE:
SNUM SNAME CITY COMM
1001 PEEL LONDON 0.12
1002 SSERRES SAN JOSE 0.13
1003 MOTIKA LONDON 0.11
1004 AXELROD NEW YORK 0.10

CUSTOMERS:
CNUM CNAME CITY RATING SNUM
2001 HOFFMAN LONDON 100 1001
2002 GIOVANNI ROME 200 1003
2003 LIU SAN JOSE 200 1002
2004 GRASS SAN JOSE 300 1002

ORDERS:
ONUM AMT ODATE CNUM
3001 18.69 10/03/90 2001
3003 767.19 12/08/90 2001
3002 1900.10 15/01/91 2004
3005 5150.45 03/10/90 2003
3006 1098.16 02/03/92 2002

6) Find the largest order taken by each salesperson.

7) Find which salespeople currently have orders in the Orders table.

8) List names of all customers matched with the salespeople serving them.

9) Count the orders of each of the salespeople and output the result descending order.
10) List the customer table if and only if one or more of the customer located in San Jose.

11) Display customer name, order amount, year for largest order amount taken in each
year.

12) Find all the customers with orders on October 3.


SQL Assignments

Day 1

1) Type the following select statement at SQL prompt and examine the output.
Select last_name, job_id, salary as sal from employees;

2) Type the following select statement at SQL prompt and examine the output.
Select * from job_grades;

3) There are four coding errors in this statement. Can you identify them?
Select employee_id, last_name sal x 12 annual salary from employees;

4) Display the structure of the departments table.

5) Display the last name concatenated with the job ID, separated by a comma and space,
and name the column Employee and Title.

6) Display department numbers and names from department table.

7) Retrieve the list of employees names, employee ID, job and salary from EMP table.

8) Retrieve the list of names, jobs with “Designation” heading , salary with “Basic
Salary” heading from EMP table.

9) List out the names of all Tables.

10) Display employee name, job, 8% on sal as bonus , 9.75% on salary as DA.

11) Display employee name, job, hiredate, salary, 9.25% on salary as Income Tax and
Net Salary ( salary – Tax ).

12) Display employee name, salary, 8% on salary as bonus and 2% as Tax on (salary +
bonus).

13) Display the following formats


a) {ename} working as a {job} with basic salary {sal}
b) {ename} working in {deptno} department as a {job} with basic {sal}
c) The employee {ename}({job}) paying Income Tax {sal*9.75/100}
14) Produce the following output.
EMPLOYEE
----------------
SMITH(Clerk)
ALLEN(Salesman)
WARD(Salesman)
JONES(Manager)
MARTIN(Salesman) and so on………………

15) Display all employees who were hired during 1983.

16) Display details who are working in the departments 10, 20 as a MANAGER.

17) Develop a query that will accept a given job name. Execute the query a number of
times to test.

18) Display the name, job, salary and bonus (8 % of salary) with “BONUS” heading and
output should be in the order of bonus.

19) Display employees details who are not getting commission. Write queries in possible
methods.

20) Display details whose employee name contain ‘E’ as a second character from end of
the string.

21) Display details whose employee name contain “S” or “s”.

22) Display employee name, job, salary, commission and total salary (sal + comm) with
ordering highest total salary first.

23) Find the most recently hired employees in each department.

24) Show all data of the clerks who have been hired after the year 1997.

25) Show the last name, job, salary, and commission of those employees who earn
commission. Sort the data by the salary in descending order.

26) Show the employees that have no commission with a 10% raise in their salary (round
off the salaries).

27) Show those employees that have a name starting with J, K, L, or M.


Day 2

1)

You might also like