SQL Queries Problems And Solutions Frequently Asking in Top IT Companies
Most of the SQL queries interview questions we have filtered out of interviews held by top IT MNC. So you’ll gain real-time experience by going through them.
Sample Table – Worker
WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT
001 Monika Arora 100000 2014-02-20 09:00:00 HR
002 Niharika Verma 80000 2014-06-11 09:00:00 Admin
003 Vishal Singhal 300000 2014-02-20 09:00:00 HR
004 Amitabh Singh 500000 2014-02-20 09:00:00 Admin
005 Vivek Bhati 500000 2014-06-11 09:00:00 Admin
006 Vipul Diwan 200000 2014-06-11 09:00:00 Account
007 Satish Kumar 75000 2014-01-20 09:00:00 Account
008 Geetika Chauhan 90000 2014-04-11 09:00:00 Admin
Sample Table – Bonus
WORKER_REF_ID BONUS_DATE BONUS_AMOUNT
1 2016-02-20 00:00:00 5000
2 2016-06-11 00:00:00 3000
3 2016-02-20 00:00:00 4000
1 2016-02-20 00:00:00 4500
2 2016-06-11 00:00:00 3500
Sample Table – Title
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM
1 Manager 2016-02-20 00:00:00
2 Executive 2016-06-11 00:00:00
8 Executive 2016-06-11 00:00:00
5 Manager 2016-06-11 00:00:00
4 Asst. Manager 2016-06-11 00:00:00
7 Executive 2016-06-11 00:00:00
6 Lead 2016-06-11 00:00:00
3 Lead 2016-06-11 00:00:00
Write An SQL Query To Fetch “FIRST_NAME” From Worker Table Using The Alias Name As <WORKER_NAME>.
Solution:
The required query is:
Select FIRST_NAME AS WORKER_NAME from Worker;
Write An SQL Query To Fetch “FIRST_NAME” From Worker Table In Upper Case.
Solution:
The required query is:
Select upper(FIRST_NAME) from Worker;
Write An SQL Query To Fetch Unique Values Of DEPARTMENT From Worker Table.
Solution:
The required query is:
Select distinct DEPARTMENT from Worker;
Write An SQL Query To Print First Three Characters Of FIRST_NAME From Worker Table.
Solution:
The required query is:
Select substring(FIRST_NAME,1,3) from Worker;
Write An SQL Query To Find The Position Of The Alphabet (‘A’) In The First Name Column ‘Amitabh’ From Worker Table.
Solution:
The required query is:
Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME = 'Amitabh';
Notes.
The INSTR method is in case-sensitive by default.
Using Binary operator will make INSTR work as the case-sensitive function.
Write An SQL Query To Print The FIRST_NAME From Worker Table After Removing White Spaces From The Right Side.
Solution:
The required query is:
Select RTRIM(FIRST_NAME) from Worker;
Write An SQL Query To Print The DEPARTMENT From Worker Table After Removing White Spaces From The Left Side.
Solution:
The required query is:
Select LTRIM(DEPARTMENT) from Worker;
Write An SQL Query That Fetches The Unique Values Of DEPARTMENT From Worker Table And Prints Its Length.
Solution:
The required query is:
Select distinct length(DEPARTMENT) from Worker;
Write An SQL Query To Print The FIRST_NAME From Worker Table After Replacing ‘A’ With ‘A’.
Solution:
The required query is:
Select REPLACE(FIRST_NAME,'a','A') from Worker;
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.
Solution:
The required query is:
Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Worker;
Write An SQL Query To Print All Worker Details From The Worker Table Order By FIRST_NAME Ascending.
Solution:
The required query is:
Select * from Worker order by FIRST_NAME asc;
Write An SQL Query To Print All Worker Details From The Worker Table Order By FIRST_NAME Ascending And DEPARTMENT Descending.
Solution:
The required query is:
Select * from Worker order by FIRST_NAME asc,DEPARTMENT desc;
Write An SQL Query To Print Details For Workers With The First Name As “Vipul” And “Satish” From Worker Table.
Solution:
The required query is:
Select * from Worker where FIRST_NAME in ('Vipul','Satish');
Write An SQL Query To Print Details Of Workers Excluding First Names, “Vipul” And “Satish” From Worker Table.
Solution:
The required query is:
Select * from Worker where FIRST_NAME not in ('Vipul','Satish');
Write An SQL Query To Print Details Of Workers With DEPARTMENT Name As “Admin”.
Solution:
The required query is:
Select * from Worker where DEPARTMENT like 'Admin%';
Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Contains ‘A’.
Solution:
The required query is:
Select * from Worker where FIRST_NAME like '%a%';
Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With ‘A’.
Solution:
The required query is:
Select * from Worker where FIRST_NAME like '%a';
Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With ‘H’ And Contains Six Alphabets.
Solution:
The required query is:
Select * from Worker where FIRST_NAME like '_____h';
Write An SQL Query To Print Details Of The Workers Whose SALARY Lies Between 100000 And 500000.
Solution:
The required query is:
Select * from Worker where SALARY between 100000 and 500000;
Write An SQL Query To Print Details Of The Workers Who Have Joined In Feb’2014.
Solution:
The required query is:
Select * from Worker where year(JOINING_DATE) = 2014 and month(JOINING_DATE) = 2;
Write An SQL Query To Fetch The Count Of Employees Working In The Department ‘Admin’.
Solution:
The required query is:
SELECT COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';
Write An SQL Query To Fetch Worker Names With Salaries >= 50000 And <= 100000.
Solution:
The required query is:
SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) As Worker_Name, Salary
FROM worker
WHERE WORKER_ID IN
(SELECT WORKER_ID FROM worker
WHERE Salary BETWEEN 50000 AND 100000);
Write An SQL Query To Fetch The No. Of Workers For Each Department In The Descending Order.
Solution:
The required query is:
SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers
FROM worker
GROUP BY DEPARTMENT
ORDER BY No_Of_Workers DESC;
Write An SQL Query To Print Details Of The Workers Who Are Also Managers.
Solution:
The required query is:
SELECT DISTINCT W.FIRST_NAME, T.WORKER_TITLE
FROM Worker W
INNER JOIN Title T
ON W.WORKER_ID = T.WORKER_REF_ID
AND T.WORKER_TITLE in ('Manager');
25. Write An SQL Query To Fetch Duplicate Records Having Matching Data In Some Fields Of A Table.
Solution:
The required query is:
SELECT WORKER_TITLE, AFFECTED_FROM, COUNT(*)
FROM Title
GROUP BY WORKER_TITLE, AFFECTED_FROM
HAVING COUNT(*) > 1;
26. Write An SQL Query To Show Only Odd Rows From A Table.
Solution:
The required query is:
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
27. Write An SQL Query To Show Only Even Rows From A Table.
Solution:
The required query is:
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) = 0;
28. Write An SQL Query To Clone A New Table From Another Table.
Solution:
The general query to clone a table with data is:
SELECT * INTO WorkerClone FROM Worker;
The general way to clone a table without information is:
SELECT * INTO WorkerClone FROM Worker WHERE 1 = 0;
An alternate way to clone a table (for MySQL) without is:
CREATE TABLE WorkerClone LIKE Worker;
29. Write An SQL Query To Fetch Intersecting Records Of Two Tables.
Solution:
The required query is:
(SELECT * FROM Worker)
INTERSECT
(SELECT * FROM WorkerClone);
30. Write An SQL Query To Show Records From One Table That Another Table Does Not Have.
Solution:
The required query is:
SELECT * FROM Worker
MINUS
SELECT * FROM Title;
31. Write An SQL Query To Show The Current Date And Time.
Solution:
Following MySQL query returns the current date:
SELECT CURDATE();
Following MySQL query returns the current date and time:
SELECT NOW();
Following SQL Server query returns the current date and time:
SELECT getdate();
Following Oracle query returns the current date and time:
SELECT SYSDATE FROM DUAL;
32. Write An SQL Query To Show The Top N (Say 10) Records Of A Table.
Solution:
Following MySQL query will return the top n records using the LIMIT method:
SELECT * FROM Worker ORDER BY Salary DESC LIMIT 10;
Following SQL Server query will return the top n records using the TOP command:
SELECT TOP 10 * FROM Worker ORDER BY Salary DESC;
Following Oracle query will return the top n records with the help of ROWNUM:
SELECT * FROM (SELECT * FROM Worker ORDER BY Salary DESC)
WHERE ROWNUM <= 10;
33. Write An SQL Query To Determine The Nth (Say N=5) Highest Salary From A Table.
Solution:
The following MySQL query returns the nth highest salary:
SELECT Salary FROM Worker ORDER BY Salary DESC LIMIT n-1,1;
The following SQL Server query returns the nth highest salary:
SELECT TOP 1 Salary
FROM (
SELECT DISTINCT TOP n Salary
FROM Worker
ORDER BY Salary DESC
)
ORDER BY Salary ASC;
34. Write An SQL Query To Determine The 5th Highest Salary Without Using TOP Or Limit Method.
Solution:
The following query is using the correlated subquery to return the 5th highest salary:
SELECT Salary
FROM Worker W1
WHERE 4 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Use the following generic method to find nth highest salary without using TOP or limit.
SELECT Salary
FROM Worker W1
WHERE n-1 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
35. Write An SQL Query To Fetch The List Of Employees With The Same Salary.
Solution:
The required query is:
Select distinct W.WORKER_ID, W.FIRST_NAME, W.Salary
from Worker W, Worker W1
where W.Salary = W1.Salary
and W.WORKER_ID != W1.WORKER_ID;
36. Write An SQL Query To Show The Second Highest Salary From A Table.
Solution:
The required query is:
Select max(Salary) from Worker
where Salary not in (Select max(Salary) from Worker);
37. Write An SQL Query To Show One Row Twice In Results From A Table.
Solution:
The required query is:
select FIRST_NAME, DEPARTMENT from worker W where W.DEPARTMENT='HR'
union all
select FIRST_NAME, DEPARTMENT from Worker W1 where W1.DEPARTMENT='HR';
38. Write An SQL Query To Fetch Intersecting Records Of Two Tables.
Solution:
The required query is:
(SELECT * FROM Worker)
INTERSECT
(SELECT * FROM WorkerClone);
39. Write An SQL Query To Fetch The First 50% Records From A Table.
Solution:
The required query is:
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
40. Write An SQL Query To Fetch The Departments That Have Less Than Five People In It.
Solution:
The required query is:
SELECT DEPARTMENT, COUNT(WORKER_ID) as 'Number of Workers' FROM Worker GROUP BY DEPARTMENT HAVING COUNT(WORKER_ID) < 5;
41. Write An SQL Query To Show All Departments Along With The Number Of People In There.
Solution:
The following query returns the expected result:
SELECT DEPARTMENT, COUNT(DEPARTMENT) as 'Number of Workers' FROM Worker GROUP BY DEPARTMENT;
42. Write An SQL Query To Show The Last Record From A Table.
Solution:
The following query will return the last record from the Worker table:
Select * from Worker where WORKER_ID = (SELECT max(WORKER_ID) from Worker);
43. Write An SQL Query To Fetch The First Row Of A Table.
Solution:
The required query is:
Select * from Worker where WORKER_ID = (SELECT min(WORKER_ID) from Worker);
44. Write An SQL Query To Fetch The Last Five Records From A Table.
Solution:
The required query is:
SELECT * FROM Worker WHERE WORKER_ID <=5
UNION
SELECT * FROM (SELECT * FROM Worker W order by W.WORKER_ID DESC) AS W1 WHERE W1.WORKER_ID <=5;
45. Write An SQL Query To Print The Name Of Employees Having The Highest Salary In Each Department.
Solution:
The required query is:
SELECT t.DEPARTMENT,t.FIRST_NAME,t.Salary from(SELECT max(Salary) as TotalSalary,DEPARTMENT from Worker group by DEPARTMENT) as TempNew
Inner Join Worker t on TempNew.DEPARTMENT=t.DEPARTMENT
and TempNew.TotalSalary=t.Salary;
46. Write An SQL Query To Fetch Three Max Salaries From A Table.
Solution:
The required query is:
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from worker b WHERE a.Salary <= b.Salary) order by a.Salary desc;
47. Write An SQL Query To Fetch Three Min Salaries From A Table.
Solution:
The required query is:
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from worker b WHERE a.Salary >= b.Salary) order by a.Salary desc;
Write An SQL Query To Fetch Nth Max Salaries From A Table.
Solution:
The required query is:
SELECT distinct Salary from worker a WHERE n >= (SELECT count(distinct Salary) from worker b WHERE a.Salary <= b.Salary) order by a.Salary desc;
Write An SQL Query To Fetch Departments Along With The Total Salaries Paid For Each Of Them.
Solution:
The required query is:
SELECT DEPARTMENT, sum(Salary) from worker group by DEPARTMENT;
Write An SQL Query To Fetch The Names Of Workers Who Earn The Highest Salary.
Solution:
The required query is:
SELECT FIRST_NAME, SALARY from Worker WHERE SALARY=(SELECT max(SALARY) from Worker);
Most of the SQL queries interview questions we have filtered out of interviews held by top IT MNC. So you’ll gain real-time experience by going through them.
Sample Table – Worker
WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT
001 Monika Arora 100000 2014-02-20 09:00:00 HR
002 Niharika Verma 80000 2014-06-11 09:00:00 Admin
003 Vishal Singhal 300000 2014-02-20 09:00:00 HR
004 Amitabh Singh 500000 2014-02-20 09:00:00 Admin
005 Vivek Bhati 500000 2014-06-11 09:00:00 Admin
006 Vipul Diwan 200000 2014-06-11 09:00:00 Account
007 Satish Kumar 75000 2014-01-20 09:00:00 Account
008 Geetika Chauhan 90000 2014-04-11 09:00:00 Admin
Sample Table – Bonus
WORKER_REF_ID BONUS_DATE BONUS_AMOUNT
1 2016-02-20 00:00:00 5000
2 2016-06-11 00:00:00 3000
3 2016-02-20 00:00:00 4000
1 2016-02-20 00:00:00 4500
2 2016-06-11 00:00:00 3500
Sample Table – Title
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM
1 Manager 2016-02-20 00:00:00
2 Executive 2016-06-11 00:00:00
8 Executive 2016-06-11 00:00:00
5 Manager 2016-06-11 00:00:00
4 Asst. Manager 2016-06-11 00:00:00
7 Executive 2016-06-11 00:00:00
6 Lead 2016-06-11 00:00:00
3 Lead 2016-06-11 00:00:00
Write An SQL Query To Fetch “FIRST_NAME” From Worker Table Using The Alias Name As <WORKER_NAME>.
Solution:
The required query is:
Select FIRST_NAME AS WORKER_NAME from Worker;
Write An SQL Query To Fetch “FIRST_NAME” From Worker Table In Upper Case.
Solution:
The required query is:
Select upper(FIRST_NAME) from Worker;
Write An SQL Query To Fetch Unique Values Of DEPARTMENT From Worker Table.
Solution:
The required query is:
Select distinct DEPARTMENT from Worker;
SQL Queries Problems And Solutions Frequently Asking in Top IT Companies |
Write An SQL Query To Print First Three Characters Of FIRST_NAME From Worker Table.
Solution:
The required query is:
Select substring(FIRST_NAME,1,3) from Worker;
Write An SQL Query To Find The Position Of The Alphabet (‘A’) In The First Name Column ‘Amitabh’ From Worker Table.
Solution:
The required query is:
Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME = 'Amitabh';
Notes.
The INSTR method is in case-sensitive by default.
Using Binary operator will make INSTR work as the case-sensitive function.
Write An SQL Query To Print The FIRST_NAME From Worker Table After Removing White Spaces From The Right Side.
Solution:
The required query is:
Select RTRIM(FIRST_NAME) from Worker;
Write An SQL Query To Print The DEPARTMENT From Worker Table After Removing White Spaces From The Left Side.
Solution:
The required query is:
Select LTRIM(DEPARTMENT) from Worker;
Write An SQL Query That Fetches The Unique Values Of DEPARTMENT From Worker Table And Prints Its Length.
Solution:
The required query is:
Select distinct length(DEPARTMENT) from Worker;
Write An SQL Query To Print The FIRST_NAME From Worker Table After Replacing ‘A’ With ‘A’.
Solution:
The required query is:
Select REPLACE(FIRST_NAME,'a','A') from Worker;
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.
Solution:
The required query is:
Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Worker;
Write An SQL Query To Print All Worker Details From The Worker Table Order By FIRST_NAME Ascending.
Solution:
The required query is:
Select * from Worker order by FIRST_NAME asc;
Write An SQL Query To Print All Worker Details From The Worker Table Order By FIRST_NAME Ascending And DEPARTMENT Descending.
Solution:
The required query is:
Select * from Worker order by FIRST_NAME asc,DEPARTMENT desc;
Write An SQL Query To Print Details For Workers With The First Name As “Vipul” And “Satish” From Worker Table.
Solution:
The required query is:
Select * from Worker where FIRST_NAME in ('Vipul','Satish');
Write An SQL Query To Print Details Of Workers Excluding First Names, “Vipul” And “Satish” From Worker Table.
Solution:
The required query is:
Select * from Worker where FIRST_NAME not in ('Vipul','Satish');
Write An SQL Query To Print Details Of Workers With DEPARTMENT Name As “Admin”.
Solution:
The required query is:
Select * from Worker where DEPARTMENT like 'Admin%';
Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Contains ‘A’.
Solution:
The required query is:
Select * from Worker where FIRST_NAME like '%a%';
Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With ‘A’.
Solution:
The required query is:
Select * from Worker where FIRST_NAME like '%a';
Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With ‘H’ And Contains Six Alphabets.
Solution:
The required query is:
Select * from Worker where FIRST_NAME like '_____h';
Write An SQL Query To Print Details Of The Workers Whose SALARY Lies Between 100000 And 500000.
Solution:
The required query is:
Select * from Worker where SALARY between 100000 and 500000;
Write An SQL Query To Print Details Of The Workers Who Have Joined In Feb’2014.
Solution:
The required query is:
Select * from Worker where year(JOINING_DATE) = 2014 and month(JOINING_DATE) = 2;
Write An SQL Query To Fetch The Count Of Employees Working In The Department ‘Admin’.
Solution:
The required query is:
SELECT COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';
Write An SQL Query To Fetch Worker Names With Salaries >= 50000 And <= 100000.
Solution:
The required query is:
SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) As Worker_Name, Salary
FROM worker
WHERE WORKER_ID IN
(SELECT WORKER_ID FROM worker
WHERE Salary BETWEEN 50000 AND 100000);
Write An SQL Query To Fetch The No. Of Workers For Each Department In The Descending Order.
Solution:
The required query is:
SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers
FROM worker
GROUP BY DEPARTMENT
ORDER BY No_Of_Workers DESC;
Write An SQL Query To Print Details Of The Workers Who Are Also Managers.
Solution:
The required query is:
SELECT DISTINCT W.FIRST_NAME, T.WORKER_TITLE
FROM Worker W
INNER JOIN Title T
ON W.WORKER_ID = T.WORKER_REF_ID
AND T.WORKER_TITLE in ('Manager');
25. Write An SQL Query To Fetch Duplicate Records Having Matching Data In Some Fields Of A Table.
Solution:
The required query is:
SELECT WORKER_TITLE, AFFECTED_FROM, COUNT(*)
FROM Title
GROUP BY WORKER_TITLE, AFFECTED_FROM
HAVING COUNT(*) > 1;
26. Write An SQL Query To Show Only Odd Rows From A Table.
Solution:
The required query is:
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
27. Write An SQL Query To Show Only Even Rows From A Table.
Solution:
The required query is:
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) = 0;
28. Write An SQL Query To Clone A New Table From Another Table.
Solution:
The general query to clone a table with data is:
SELECT * INTO WorkerClone FROM Worker;
The general way to clone a table without information is:
SELECT * INTO WorkerClone FROM Worker WHERE 1 = 0;
An alternate way to clone a table (for MySQL) without is:
CREATE TABLE WorkerClone LIKE Worker;
29. Write An SQL Query To Fetch Intersecting Records Of Two Tables.
Solution:
The required query is:
(SELECT * FROM Worker)
INTERSECT
(SELECT * FROM WorkerClone);
30. Write An SQL Query To Show Records From One Table That Another Table Does Not Have.
Solution:
The required query is:
SELECT * FROM Worker
MINUS
SELECT * FROM Title;
31. Write An SQL Query To Show The Current Date And Time.
Solution:
Following MySQL query returns the current date:
SELECT CURDATE();
Following MySQL query returns the current date and time:
SELECT NOW();
Following SQL Server query returns the current date and time:
SELECT getdate();
Following Oracle query returns the current date and time:
SELECT SYSDATE FROM DUAL;
32. Write An SQL Query To Show The Top N (Say 10) Records Of A Table.
Solution:
Following MySQL query will return the top n records using the LIMIT method:
SELECT * FROM Worker ORDER BY Salary DESC LIMIT 10;
Following SQL Server query will return the top n records using the TOP command:
SELECT TOP 10 * FROM Worker ORDER BY Salary DESC;
Following Oracle query will return the top n records with the help of ROWNUM:
SELECT * FROM (SELECT * FROM Worker ORDER BY Salary DESC)
WHERE ROWNUM <= 10;
33. Write An SQL Query To Determine The Nth (Say N=5) Highest Salary From A Table.
Solution:
The following MySQL query returns the nth highest salary:
SELECT Salary FROM Worker ORDER BY Salary DESC LIMIT n-1,1;
The following SQL Server query returns the nth highest salary:
SELECT TOP 1 Salary
FROM (
SELECT DISTINCT TOP n Salary
FROM Worker
ORDER BY Salary DESC
)
ORDER BY Salary ASC;
34. Write An SQL Query To Determine The 5th Highest Salary Without Using TOP Or Limit Method.
Solution:
The following query is using the correlated subquery to return the 5th highest salary:
SELECT Salary
FROM Worker W1
WHERE 4 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Use the following generic method to find nth highest salary without using TOP or limit.
SELECT Salary
FROM Worker W1
WHERE n-1 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
35. Write An SQL Query To Fetch The List Of Employees With The Same Salary.
Solution:
The required query is:
Select distinct W.WORKER_ID, W.FIRST_NAME, W.Salary
from Worker W, Worker W1
where W.Salary = W1.Salary
and W.WORKER_ID != W1.WORKER_ID;
36. Write An SQL Query To Show The Second Highest Salary From A Table.
Solution:
The required query is:
Select max(Salary) from Worker
where Salary not in (Select max(Salary) from Worker);
37. Write An SQL Query To Show One Row Twice In Results From A Table.
Solution:
The required query is:
select FIRST_NAME, DEPARTMENT from worker W where W.DEPARTMENT='HR'
union all
select FIRST_NAME, DEPARTMENT from Worker W1 where W1.DEPARTMENT='HR';
38. Write An SQL Query To Fetch Intersecting Records Of Two Tables.
Solution:
The required query is:
(SELECT * FROM Worker)
INTERSECT
(SELECT * FROM WorkerClone);
39. Write An SQL Query To Fetch The First 50% Records From A Table.
Solution:
The required query is:
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
40. Write An SQL Query To Fetch The Departments That Have Less Than Five People In It.
Solution:
The required query is:
SELECT DEPARTMENT, COUNT(WORKER_ID) as 'Number of Workers' FROM Worker GROUP BY DEPARTMENT HAVING COUNT(WORKER_ID) < 5;
41. Write An SQL Query To Show All Departments Along With The Number Of People In There.
Solution:
The following query returns the expected result:
SELECT DEPARTMENT, COUNT(DEPARTMENT) as 'Number of Workers' FROM Worker GROUP BY DEPARTMENT;
42. Write An SQL Query To Show The Last Record From A Table.
Solution:
The following query will return the last record from the Worker table:
Select * from Worker where WORKER_ID = (SELECT max(WORKER_ID) from Worker);
43. Write An SQL Query To Fetch The First Row Of A Table.
Solution:
The required query is:
Select * from Worker where WORKER_ID = (SELECT min(WORKER_ID) from Worker);
44. Write An SQL Query To Fetch The Last Five Records From A Table.
Solution:
The required query is:
SELECT * FROM Worker WHERE WORKER_ID <=5
UNION
SELECT * FROM (SELECT * FROM Worker W order by W.WORKER_ID DESC) AS W1 WHERE W1.WORKER_ID <=5;
45. Write An SQL Query To Print The Name Of Employees Having The Highest Salary In Each Department.
Solution:
The required query is:
SELECT t.DEPARTMENT,t.FIRST_NAME,t.Salary from(SELECT max(Salary) as TotalSalary,DEPARTMENT from Worker group by DEPARTMENT) as TempNew
Inner Join Worker t on TempNew.DEPARTMENT=t.DEPARTMENT
and TempNew.TotalSalary=t.Salary;
46. Write An SQL Query To Fetch Three Max Salaries From A Table.
Solution:
The required query is:
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from worker b WHERE a.Salary <= b.Salary) order by a.Salary desc;
47. Write An SQL Query To Fetch Three Min Salaries From A Table.
Solution:
The required query is:
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from worker b WHERE a.Salary >= b.Salary) order by a.Salary desc;
Write An SQL Query To Fetch Nth Max Salaries From A Table.
Solution:
The required query is:
SELECT distinct Salary from worker a WHERE n >= (SELECT count(distinct Salary) from worker b WHERE a.Salary <= b.Salary) order by a.Salary desc;
Write An SQL Query To Fetch Departments Along With The Total Salaries Paid For Each Of Them.
Solution:
The required query is:
SELECT DEPARTMENT, sum(Salary) from worker group by DEPARTMENT;
Write An SQL Query To Fetch The Names Of Workers Who Earn The Highest Salary.
Solution:
The required query is:
SELECT FIRST_NAME, SALARY from Worker WHERE SALARY=(SELECT max(SALARY) from Worker);
Post a Comment