CBSE Class 12 Computer Science - Solved SQL Queries
CBSE Class 12 Computer Science - Solved SQL Queries
com
Table EMP
1. Insert a record with suitable data in the table emp, having emp as the Hiredate.
SQL> insert into emp values (3008,18,'Xavier', 7782, sysdate, 3250, NULL, 20);
3. To create a table DEPT30 to hold the all information of employees with deptno 30
SQL> create table dept30 as select * from emp where deptno=30;
Table created.
4. Display names of all employee whose name include either of the substring TH or
LL
SQL> select ename from emp where ename like '%TH%' or ename like '%LL%'
ENAME
----------
SMITH
ALLEN
MILLER
5. Display data for all CLERKS who earn between 1000 and 2000;
SQL> select * from emp where job='CLERK' AND SAL BETWEEN 1000 AND 2000;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ---------
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7. Display the name of the employee who earns the maximum salary.
SQL> select ename from emp where sal=(select max(sal) from emp
ENAME
----------
KING
8. Write a SQL statement to list empno, empname, deptno for all the employees. The
information should be sorted on empname;
SQL> Select empno, ename, deptno from emp order by ename;
10. Display the name of the employee who earns the maximum salary.
SQL> select ename from emp where sal=(select min(sal) from emp);
ENAME
----------
SMITH
11. To display deptno, job, empname in reverse order of salary from emp.
SQL> select deptno, job, ename from emp order by sal
DEPTNO JOB ENAME
--------- --------- ----------
20 CLERK SMITH
30 CLERK JAMES
20 CLERK ADAMS
30 SALESMAN WARD
30 SALESMAN MARTIN
10 CLERK MILLER
30 SALESMAN TURNER
30 SALESMAN ALLEN
10 MANAGER CLARK
30 MANAGER BLAKE
20 MANAGER JONES
20 ANALYST SCOTT
20 ANALYST FORD
20 Xavier 18
10 PRESIDENT KING
15 rows selected.
12. Show the average salary for all departments with more than 3 people for a job.
SQL> select deptno, avg(sal) from emp group by deptno having count(*) > 3;
DEPTNO AVG(SAL)
--------- ---------
20 2354.1667
30 1566.6667
13. Create a view deptno20 with empname and salary of employees for dept 20
SQL> create view deptno20 as select ename, sal from emp where deptno=20;
View created.
14. Write a SQL statement to find out the total number of employees from emp table.
SQL> select count(*) from emp;
COUNT(*)
---------
15
15. Write a suitable sql statement to display employees name, salary and location of all
the employees working in new york in the following format
SQL> select ename "EmpName", sal "Salary", dept.loc "Location" from emp, dept where
EMP.DEPTNO=DEPT.
EmpName Salary Location
---------- --------- -------------
CLARK 2450 NEW YORK
KING 5000 NEW YORK
MILLER 1300 NEW YORK
18. Display names of all employee whose name is at least 4 characters long;
SQL> select ename from emp where length(ename)>=4;
ENAME
----------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
14 rows selected.
TO_CHAR(
--------
12:01:01
21. Write a SQL query to increase the salary of each employee by 200.
SQL> update emp set sal = sal + 200 ;
15 rows updated.