Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

SQL Assignment

The document contains details about creating sample database "ORG" with tables like Worker, Bonus and Title. It provides sample data inserted into these tables. A set of SQL queries are listed to retrieve specific data from these tables.

Uploaded by

vasundhara
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

SQL Assignment

The document contains details about creating sample database "ORG" with tables like Worker, Bonus and Title. It provides sample data inserted into these tables. A set of SQL queries are listed to retrieve specific data from these tables.

Uploaded by

vasundhara
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 105

CREATE DATABASE ORG; Sample Table – Worker

SHOW DATABASES; WORKER_ID FIRST_NAME


USE ORG;
1 Monika
CREATE TABLE Worker ( 2 Niharika
WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 3 Vishal
FIRST_NAME CHAR(25),
LAST_NAME CHAR(25), 4 Amitabh
SALARY INT(15),
JOINING_DATE DATETIME, 5 Vivek
DEPARTMENT CHAR(25)
); 6 Vipul
7 Satish
INSERT INTO Worker 8 Geetika
(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT)
VALUES
(001, 'Monika', 'Arora', 100000, '14-02-20 09.00.00', 'HR'),
(002, 'Niharika', 'Verma', 80000, '14-06-11 09.00.00', 'Admin'), Sample Table – Bonus
(003, 'Vishal', 'Singhal', 300000, '14-02-20 09.00.00', 'HR'),
(004, 'Amitabh', 'Singh', 500000, '14-02-20 09.00.00', 'Admin'), WORKER_REF_ID BONUS_DATE
(005, 'Vivek', 'Bhati', 500000, '14-06-11 09.00.00', 'Admin'),
(006, 'Vipul', 'Diwan', 200000, '14-06-11 09.00.00', 'Account'), 1 2/20/2016 0:00
(007, 'Satish', 'Kumar', 75000, '14-01-20 09.00.00', 'Account'),
(008, 'Geetika', 'Chauhan', 90000, '14-04-11 09.00.00', 'Admin');
2 6/11/2016 0:00
CREATE TABLE Bonus (
WORKER_REF_ID INT,
BONUS_AMOUNT INT(10), 3 2/20/2016 0:00
BONUS_DATE DATETIME, 1 2/20/2016 0:00
FOREIGN KEY (WORKER_REF_ID) 2 6/11/2016 0:00
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
); Sample Table – Title
WORKER_REF_ID WORKER_TITLE
INSERT INTO Bonus
(WORKER_REF_ID, BONUS_AMOUNT, BONUS_DATE) VALUES
(001, 5000, '16-02-20'), 1 Manager
(002, 3000, '16-06-11'),
(003, 4000, '16-02-20'),
(001, 4500, '16-02-20'),
(002, 3500, '16-06-11'); 2 Executive

CREATE TABLE Title ( 8 Executive


WORKER_REF_ID INT,
WORKER_TITLE CHAR(25),
AFFECTED_FROM DATETIME,
FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID) 5 Manager
ON DELETE CASCADE
);

INSERT INTO Title 4 Asst. Manager


(WORKER_REF_ID, WORKER_TITLE, AFFECTED_FROM) VALUES
(001, 'Manager', '2016-02-20 00:00:00'),
(002, 'Executive', '2016-06-11 00:00:00'),
(008, 'Executive', '2016-06-11 00:00:00'), 7 Executive
(005, 'Manager', '2016-06-11 00:00:00'), 6 Lead
(004, 'Asst. Manager', '2016-06-11 00:00:00'), 3 Lead
(007, 'Executive', '2016-06-11 00:00:00'),
(006, 'Lead', '2016-06-11 00:00:00'),
(003, 'Lead', '2016-06-11 00:00:00');
1

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 only odd rows from a table.


Write an SQL query to show only even rows from a table.
Write an SQL query to clone a new table from another table.
Write an SQL query to fetch intersecting records of two tables

Write an SQL query to show records from one table that another table does not have.

Write an SQL query to show the current date and time.

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 intersecting records of two tables.

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 show the last record from a table.

Write an SQL query to fetch the first row of a table.

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 three max salaries from a table.

Write an SQL query to fetch three min salaries from a table.

Write an SQL query to fetch nth max salaries from a table.

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 FIRST_NAME||' '||LAST_NAME as "COMPLTE_NAME" from WORKER;


SELECT * FROM WORKER ORDER BY FIRST_NAME ASC;

SELECT * FROM WORKER ORDER BY FIRST_NAME ASC, DEPARTMENT DESC;

SELECT FIRST_NAME from WORKER WHERE FIRST_NAME IN('Vipul','Satish');

SELECT FIRST_NAME from WORKER WHERE FIRST_NAME NOT


IN('Vipul','Satish');
SELECT * FROM WORKER WHERE DEPARTMENT ='Admin'
SELECT * FROM WORKER WHERE FIRST_NAME LIKE '%a%';
SELECT * FROM WORKER WHERE FIRST_NAME LIKE '%a';
select * from worker where first_name like '_____h';
SELECT * FROM WORKER WHERE SALARY BETWEEN 100000 AND 500000;
select * from worker where JOINING_DATE between '02-01-2014' AND
'02-28-2014' order by JOINING_DATE;

SELECT COUNT (*) FROM WORKER WHERE DEPARTMENT = 'Admin'; / SELECT


COUNT(DEPARTMENT) FROM WORKER WHERE DEPARTMENT ='Admin'

SELECT FIRST_NAME FROM WORKER WHERE SALARY >=50000 AND SALARY


<=100000
SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers
FROM worker
GROUP BY DEPARTMENT
ORDER BY No_Of_Workers DESC;

SELECT DISTINCT W.FIRST_NAME, W.LAST_NAME,W.SALARY,


T.WORKER_TITLE FROM WORKER W INNER JOIN TITLE T ON
W.WORKER_ID=T.WORKER_REF_ID AND T.WORKER_TITLE IN('Manager');

SELECT WORKER_TITLE ,AFFECTED_FROM FROM TITLE GROUP BY


WORKER_TITLE, AFFECTED_FROM HAVING COUNT(*) >1
Select * from WORKER where WORKER_ID % 2 != 0
Select * from WORKER where WORKER_ID % 2 = 0
CREATE TABLE CLONE AS SELECT * FROM WORKER;
SELECT * FROM WORKER INTERSECT SELECT * FROM CLONE;

select WORKER_ID, FIRST_NAME, LAST_NAME from WORKER


where not exists(select * from BONUS where WORKER_ID=WORKER_REF_ID);

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 SALARY FROM WORKER W1 WHERE 4=( SELECT


COUNT(DISTINCT(W2.SALARY)) FROM WORKER W2 WHERE W2.SALARY >
W1.SALARY)
SELECT DISTINCT W1.FIRST_NAME, W2.SALARY FROM WORKER W1, WORKER
W2 WHERE W1.SALARY = W2.SALARY AND W1.WORKER_ID!
=W2.WORKER_ID;

SELECT DISTINCT SALARY FROM WORKER ORDER BY SALARY DESC LIMIT 1,1;
SELECT * FROM WORKER UNION ALL SELECT * FROM WORKER;

SELECT * FROM WORKER INTERSECT SELECT * FROM CLONE;

SELECT * FROM WORKER WHERE WORKER_ID<= (SELECT


COUNT(WORKER_ID)/2 FROM WORKER);
SELECT DEPARTMENT, COUNT(WORKER_ID) FROM WORKER GROUP BY
DEPARTMENT HAVING COUNT(WORKER_ID)<5;
SELECT DEPARTMENT, COUNT(DEPARTMENT) AS 'No of Workers' FROM
WORKER GROUP BY DEPARTMENT;
SELECT * FROM WORKER WHERE WORKER_ID =(SELECT MAX(WORKER_ID)
FROM WORKER);
SELECT * FROM WORKER WHERE WORKER_ID =(SELECT MIN(WORKER_ID)
FROM WORKER);
SELECT * FROM WORKER WHERE WORKER_ID > (SELECT COUNT(WORKER_ID)
FROM WORKER) -5;

SELECT FIRST_NAME||LAST_NAME AS 'Worker Nmae' ,DEPARTMENT,SALARY


FROM WORKER WHERE(DEPARTMENT,SALARY) IN(SELECT
DEPARTMENT,MAX(SALARY) FROM WORKER GROUP BY DEPARTMENT);

SELECT DISTINCT SALARY FROM WORKER W1 WHERE 3>=(SELECT


COUNT(DISTINCT SALARY) FROM WORKER W2 WHERE
W1.SALARY<=W2.SALARY) ORDER BY W1.SALARY DESC;
SELECT DISTINCT SALARY FROM WORKER W1 WHERE 3>=(SELECT
COUNT(DISTINCT SALARY) FROM WORKER W2 WHERE
W1.SALARY>=W2.SALARY) ORDER BY W1.SALARY DESC;
SELECT DISTINCT SALARY FROM WORKER W1 WHERE n>=(SELECT
COUNT(DISTINCT SALARY) FROM WORKER W2 WHERE
W1.SALARY<=W2.SALARY) ORDER BY W1.SALARY DESC;
SELECT DEPARTMENT,SUM(SALARY) FROM WORKER GROUP BY DEPARTMENT;
SELECT FIRST_NAME, SALARY from Worker WHERE SALARY=(SELECT
max(SALARY) from Worker);
27
28

28

29
27

27
38
29

29

You might also like