SQL Assignment
SQL Assignment
2
3
4
6
7
8
9
11 11
10
12
13
14
15
16
17
18
19
21
22
23
24
25
32
33
34
35
36
37
39
40
41
42
43
44
45
46
47
49
50
27
Sample DB - ORG
Sample Table – Worker S No
LAST_NAME SALARY JOINING_DATE DEPARTMENT 1
Arora 100000 2/20/2014 9:00 HR 2
Verma 80000 6/11/2014 9:00 Admin 3
Singhal 300000 2/20/2014 9:00 HR 4
Singh 500000 2/20/2014 9:00 Admin
5
Bhati 500000 6/11/2014 9:00 Admin 6
Diwan 200000 6/11/2014 9:00 Account 7
Kumar 75000 1/20/2014 9:00 Account 8
Chauhan 90000 4/11/2014 9:00 Admin 9
10
e Table – Bonus 11
BONUS_AMOUNT
12
5000
13
3000
14
4000 15
4500 16
3500 17
18
ple Table – Title 19
AFFECTED_FROM
20
2/20/2016 0:00
21
6/11/2016 0:00
22
6/11/2016 0:00
23
6/11/2016 0:00
24
6/11/2016 0:00
25
6/11/2016 0:00 26
6/11/2016 0:00 27
6/11/2016 0:00 28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
ample DB - ORG
Question
Write an SQL query to fetch “FIRST_NAME” from Worker table using the alias name as <WORKER_NAME>
Write an SQL query to fetch “FIRST_NAME” from Worker table in upper case.
Write an SQL query to fetch unique values of DEPARTMENT from Worker table.
Write an SQL query to print the first three characters of FIRST_NAME from Worker table.
Write an SQL query to find the position of the alphabet (‘a’) in the first name column ‘Amitabh’ from Worker table.
Write an SQL query to print the FIRST_NAME from Worker table after removing white spaces from the right side.
Write an SQL query to print the DEPARTMENT from Worker table after removing white spaces from the left side.
Write an SQL query that fetches the unique values of DEPARTMENT from Worker table and prints its length.
Write an SQL query to print the FIRST_NAME from Worker table after replacing ‘a’ with ‘A’.
Write an SQL query to print the FIRST_NAME and LAST_NAME from Worker table into a single column
COMPLETE_NAME. A space char should separate them.
Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending.
Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending and
DEPARTMENT Descending.
Write an SQL query to print details for Workers with the first name as “Vipul” and “Satish” from Worker table.
Write an SQL query to print details of workers excluding first names, “Vipul” and “Satish” from Worker table.
Write an SQL query to print details of Workers with DEPARTMENT name as “Admin”.
Write an SQL query to print details of the Workers whose FIRST_NAME contains ‘a’.
Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘a’.
Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘h’ and contains six alphabets.
Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000.
Write an SQL query to print details of the Workers who have joined in Feb’2014.
Write an SQL query to fetch the count of employees working in the department ‘Admin’
Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.
Write an SQL query to fetch the no. of workers for each department in the descending order.
Write an SQL query to print details of the Workers who are also Managers.
Write an SQL query to fetch duplicate records having matching data in some fields of a table.
Write an SQL query to show records from one table that another table does not have.
Write an SQL query to show the top n (say 10) records of a table.
Write an SQL query to determine the nth (say n=5) highest salary from a table.
Write an SQL query to determine the 5th highest salary without using TOP or limit method.
Write an SQL query to fetch the list of employees with the same salary.
Write an SQL query to show the second highest salary from a table.
Write an SQL query to show one row twice in results from a table.
Write an SQL query to fetch the first 50% records from a table.
Write an SQL query to fetch the departments that have less than five people in it.
Write an SQL query to show all departments along with the number of people in there.
Write an SQL query to fetch the last five records from a table.
Write an SQL query to print the name of employees having the highest salary in each department
Write an SQL query to fetch departments along with the total salaries paid for each of them.
Write an SQL query to fetch the names of workers who earn the highest salary.
26
30
Solution
select FIRST_NAME from WORKER AS WORKER_NAME;
SELECT Upper(FIRST_NAME) FROM WORKER;
SELECT DISTINCT(DEPARTMENT) FROM WORKER;
SELECT SUBSTR(FIRST_NAME,1,3)FROM WORKER;
Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME =
'Amitabh';
SELECT RTRIM(FIRST_NAME) FROM WORKER;
SELECT LTRIM(DEPARTMENT) FROM WORKER;
SELECT DISTINCT LENGTH(DEPARTMENT) FROM WORKER;
SELECT REPLACE(FIRST_NAME,'a','A') FROM WORKER;
SELECT getdate();
SELECT * FROM WORKER ORDER BY SALARY DESC LIMIT 10; / SELECT * FROM
WORKER LIMIT 10;
SELECT DISTINCT SALARY FROM WORKER ORDER BY SALARY DESC LIMIT 4,1;
SELECT DISTINCT SALARY FROM WORKER ORDER BY SALARY DESC LIMIT 1,1;
SELECT * FROM WORKER UNION ALL SELECT * FROM WORKER;
28
29
27
27
38
29
29