S Q L - Notes
S Q L - Notes
2) Type the following select statement at SQL prompt and examine the output.
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;
5) Display the last name concatenated with the job ID, separated by a comma and space,
and name the column Employee and Title.
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.
SQL> SELECT ENAME, JOB DESIGNATION, SAL "Basic Salary" FROM EMP;
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).
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;
SQL> SELECT ENAME || ' working as a ' || JOB || ' with basic ' || SAL AS
"EMPLOYEE INFORMATION" FROM EMP;
SQL> SELECT ENAME || ' working in ' || DEPTNO || ' department as a ' || JOB || '
with basic ' || SAL EMP_INFO FROM EMP;
SQL> SELECT ENAME || '(' || JOB || ')' || ' paying Income Tax ' || SAL * 9.75 / 100
TAX_INFO FROM EMP;
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.
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.
20) Display details whose employee name contain ‘E’ as a second character from end of
the string.
22) Display employee name, job, salary, commission and total salary (sal + comm) with
ordering highest total salary first.
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).
SQL> SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, ROUND (SAL + SAL
* 10 / 100), COMM, DEPTNO
FROM EMP
WHERE COMM = 0 OR COMM IS NULL;
Day 2
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.
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.
7) Find out employee details whose salary is less than average salary of 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.
11) How many employees have a name that ends with an n? Create two possible
solutions.
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)
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.
Day 3
SQL> UPDATE EMP SET SAL = NVL (SAL, 0) + NVL (COMM, 0);
3) Update salary by 1000 of employees who are working in accounting department and
whose job experience is more than 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
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.
11) Display customer name, order amount, year for largest order amount taken in each
year.
SQL> SELECT *
FROM CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
O.ODATE LIKE ’03-OCT%’;
SQL Assignments
Day 5
SQL> UPDATE EMP SET SAL = NVL (SAL, 0) + NVL (COMM, 0);
3) Update salary by 1000 of employees who are working in accounting department and
whose job experience is more than 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
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.
11) Display customer name, order amount, year for largest order amount taken in each
year.
SQL> SELECT *
FROM CUSTOMERS C, ORDERS O
WHERE O.CNUM = C.CNUM AND
O.ODATE LIKE ’03-OCT%’;
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
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
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.
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;
5) Display the last name concatenated with the job ID, separated by a comma and space,
and name the column Employee and Title.
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.
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).
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.
22) Display employee name, job, salary, commission and total salary (sal + comm) with
ordering highest total salary first.
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).
1)