Top 50 Question
Top 50 Question
USE ORGIN; -- SHOW DATABASE KE BAD HAME "USE ORGIN" Per switch karna hota hai ye hamko
nahi bhulana hai it is most important
FIRST_NAME CHAR(25),
LAST_NAME CHAR(25),
SALARY INT(15),
JOINING_DATE DATETIME,
DEPARTMENT CHAR(25)
);
--
WORKER_REF_ID INT,
BONUS_AMOUNT INT(10),
BONUS_DATE DATETIME,
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);
WORKER_REF_ID INT,
WORKER_TITLE CHAR(25),
AFFECTED_FROM DATETIME,
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);
-- "iska query SQL File6 me likha gaya hai (Orgin) db me sabhi table bana hai bonus,title,worker ka "
-- Q-1. Write an SQL query to fetch "FIRST_NAME" from Worker table using the alias name as
<WORKER_NAME>.
-- Q-2. Write an SQL query to fetch "FIRST_NAME" from Worker table in upper case.
-- Q-3. Write an SQL query to fetch unique values of DEPARTMENT from Worker table.
-- Q-4. Write an SQL query to print first three Characters of FIRST_NAME from Worker table.
select substring(first_name, 1,3) from worker; -- starting se 3 character chahiye sabhi first_name
ke
-- Q-5. Write an SQL query to find the position of the alphabet ('b') in the first name column
'Amitabh' from Worker table. O/P - 6
select INSTR(first_name, 'B') from worker where first_name = 'Amitabh'; -- where clause ka use
condition ke liye kiya jata hai
-- Q-6. Write an SQL query to print the FIRST_NAME from Worker table after removing white spaces
from the right side.
select RTRIM(first_name) from worker;
-- Q-7. Write an SQL query to print the DEPARTMENT from Worker table after removing white spaces
from the left side.
-- Q-8. Write an SQL query that fetches the unique values of DEPARTMENT from Worker table and
prints its length.
-- Q-9. Write an SQL query to print the FIRST_NAME from Worker table after replacing 'a' with 'A'.
-- Q-10. Write an SQL query to print the FIRST_NAME and LAST_NAME from Workertable into a
single column COMPLETE_NAME.
-- Q-11. Write an SQL query to print all Worker details from the worker table order by FIRST_NAME
Ascending.
-- Q-12. Write an SQL query to print all worker details from the worker table order by
-- Q-13. Write an SQL query to print details for the Workers with the first name as "Vipul" and
"Satish" from Worker table.
select * from worker where first_name IN ('Vipul' , 'Satish'); -- multiple value ke liye IN ka use
karege aur single ke liye OR
-- Q-14. Write an SQL query to print details for the Workers exeluding first names, "Vipul" and
"Satish" from Worker table.
select * from worker where first_name NOT IN ('Vipul' , 'Satish');
-- Q-15. Write an SQL query to print details of Workers with DEPARTMENT name as "Admin*".
-- Q-16. Write an SQL query to print details of the Workers whose FIRST_NAME ends with 'h' and
contains six alphabets.
select * from worker where first_name LIKE '%a%'; -- a , kahi per bhi name me aya ho
-- % sign null se bhi replace hota hai yani ki koi bhi character na ho 0 characters ho ya n
character ho
-- Q-17. Write an SQL query to print details of the Workers whose FIRST_NAME ends with 'a'.
-- % a .. means last me a value jaha hoga vo name print hoga .. a% menas value(a) start me "a" jaha
hoga vo print hogi ..%a% means
-- Q-18. Write an SQL query to print details of the Workers whose FIRST_NAME ends with 'h' and
contins six alphabets.
select* from worker where first_name LIKE '_____h'; -- yaha 5 space hai aur last h hai to 5 kuch
bhi ho hame fark nahi padta bas last h hona chahiye
-- Q-19. Write an SQL query to print details of the Workers whose SALARY lies between 100000 and
500000.
-- Q-20. Write an SQL query to print details of the Workers who have joined in Feb' 2014.
-- Q-21. Write an SQL query to fetch the count of employees working in the department 'Admin'.
-- Q-23. Write an SQL query to fetch the no. of workers for each department in the deceding order.
-- Q-24. Write an SQL query to print details of the Workers who are also Managers.
-- Q-25. Write an SQL query to fetch number(more than 1) OF Difference titles in ORG.
select worker_title, count(*) as count from title group by worker_title having count > 1;
-- Q-26. Write an SQL query to show only odd rows from a table. -- 1, 3, 5 roe show karni hai
select * from worker where MOD (Worker_ID, 2) <>0; -- above and this is same working
-- Q-27. Write an SQL query to show only even rows from a table.
-- Q-28. Write an SQL query to clone a new table from another table.
-- Q-30. Write an SQL query to show records from one table that another table does not have.
-- MINUS
-- Q-31. Write an SQL query to show the current date and time.
-- DUAL
select curdate(); -- is function se current date aa jayegi(abhi ki jin din run karege vo date
aajayegi)
-- Q-32. Write an SQL query to show the top n (say 5) records of a table order by desending salary.
select * from Worker order by salary desc LIMIT 5; -- 5 chahiye to uske liye LIMIT keyword ka
use karege to top 5 value miljayegi
-- Q-33. Write an SQL query to determine the nth (say n=5) highest salary from a table.
select * from Worker order by salary desc LIMIT 4; -- pahli 4 entry aajayegi
select * from Worker order by salary desc LIMIT 4,1; -- 4 value chodkar 1 value print
karega , 4 starting ki tarah behave karega
-- shirf 5 th salary print karna hai to uper vala code likhege , 6th hight sallary chahiye to "5,1"
likhege
-- Q-34. Write an SQL query to determine the 5th highest salary without using LIMIT keyword.
WHERE 4 = (
from worker w2
);
-- Q-35. Write an SQL query to fetch the list of employees with the same salary.
select w1.* from worker w1, worker w2 where w1.salary = w2.salary and w1.worker_id !=
w2.worker_id;
-- Q-36. Write an SQL query to show the second highest salary from a table using sub-query.
-- Q-37. Write an SQL query to show one row twice in results from a table.
UNION ALL
-- Q-38. Write an SQL query to list worker_id who does not get bonus.
select worker_id from worker where worker_id not in (select worker_ref_id from bonus);
-- Q-39. Write an SQL query to fetch the first 50% records from a table.
select * from worker where worker_id <= (select count(worker_id)/2 from worker);
-- Q-40. Write an SQL query to fetch the departments that have less than 4 people in it.
-- Q-41. Write an SQL query to show all departments along with the number of people in there.
-- Q-42. Write an SQL query to show the last record from a table.
select * from worker where worker_id = (select max(worker_id) from worker); -- max nikalege
to last vala aajayega
select * from worker where worker_id = (select min(worker_id) from worker); -- min se first vali
worker_id aajayge
-- Q-44. Write an SQL query to fetch the last five records from a table.
-- Q-45. Write an SQL query to print the name of employees having the highest salary in each
department.
(select max(salary) as maxsal, department from worker group by department) temp -- temp is
alish
-- Q-46. Write an SQL query to fetch three max salaries from a table using co-related subquery
where 3 >= (select count(distinct salary) from worker w2 where w1.salary <= w2.salary) order
by w1.salary desc;
-- DRY RUN AFTER REVISING THE CORELATED SUBQUERY CONCEPT FROM LEC-9.
select distinct salary from worker order by salary desc limit 3; -- startng 3 values aajayegi
-- Q-47. Write an SQL query to fetch three min salaries from a table using co-related subquery
where 3 >= (select count(distinct salary) from worker w2 where w1.salary >= w2.salary) order
by w1.salary desc;
-- pata nahi exact answer nahi aaraha hai but syntax sahi hai
-- Q-48. Write an SQL query to fetch nth max salaries from a table.
where n >= (select count(distinct salary) from worker w2 where w1.salary <= w2.salary) order
by w1.salary desc;
-- Q-49. Write an SQL query to fetch departments along with the total salaries paid for each of them.
select department , sum(salary) as depSal from worker group by department order by depSal
desc;
-- Q-50. Write an SQL query to fetch the names of workers who earn the highest salary.
select first_name, salary from worker where salary = (select max(Salary) from worker);