SQL Top5 Questions
SQL Top5 Questions
First, create two tables for the practice which are Employee Table and
Employee Detail Table as below.
Q1(b): Write a query to retrieve the list of employees from the same city.
select E1.empname,E1.city from employee E1, employee E2 where E1.city=E2.city and E1.empid
!=E2.empid;
Q2(c): Write a query to fetch 50% records from the Employee table.
select * from employee where empid <= (select count(empid)/2 from employee)
Q3: Query to fetch the employee’s salary but replace the LAST 2 digits with ‘XX’ i.e.,
12345 will be 123XX
SELECT Salary, CONCAT(LEFT(CAST(Salary AS text), LENGTH(CAST(Salary AS text))-2), 'XX') AS
masked_number FROM Employee
Q4: Write a query to fetch even and odd rows from Employee table.
select * from employee where mod(empid,2)=0; Fetch Even rows
select * from employee where mod(empid,2)<>0; Fetch odd rows
Q5(a): Write a query to find all the Employee names whose name:
• Begin with ‘A’
• Contains ‘A’ alphabet at second place
• Contains ‘Y’ alphabet at second last place
• Ends with ‘L’ and contains 4 alphabets
• Begins with ‘V’ and ends with ‘A’
Q5(b): Write a query to find the list of Employee names which is:
• starting with vowels (a, e, i, o, or u), without duplicates
• ending with vowels (a, e, i, o, or u), without duplicates
• starting & ending with vowels (a, e, i, o, or u), without duplicates
---starting with vowels [aeiou] without duplicates
select distinct(empname) from employee where lower(empname)
similar to '[aeiou]%'
or,
select distinct(empname) from employee where lower(empname)
similar to '[aeiou]%[eioua]'
Q6: Find Nth highest salary from employee table with using the
LIMIT keywords.
select salary from employee order by salary desc limit 1 offset 1
Q7(a): Write a query to find and remove duplicate records from a table.
---finding the duplicate records from table
select empid,empname,gender,salary,city,count(*) as "duplicate count" from employee group by
empid,empname,gender,salary,city having count(*)>1;
Q8: Show the employee with the highest salary for each project.
SELECT E1.project,MAX(E.salary) AS "Maximum Project Salary" FROM employee AS E INNER JOIN
employeedetail AS E1 ON E.empid = E1.empid GROUP BY E1.project order by E1.project
Q9: Query to find the total count of employees joined each year.
SELECT EXTRACT('year' FROM doj) as Year, count(*) FROM employeedetail group by Year order by
Year;
Q10: Create 3 groups based on salary col, salary less than 1L is low, between 1 -
2L is medium and above 2L is High.
select Salary,
case when Salary >200000 Then 'High' when Salary >=100000 and Salary <=200000 Then 'Medium' else
'Low' end as "Salary Status" from employee;
Q11. Query to pivot the data in the Employee table and retrieve the total
salary for each city.
The result should display the EmpID, EmpName, and separate columns for each
city
(Mathura, Pune, Delhi), containing the corresponding total salary.
select empid,empname,
sum(case when city='Mathura' then salary end) as Mathura,
sum(case when city='Pune' then salary end) as Pune,
sum(case when city='Delhi' then salary end) as Delhi,
sum(case when city='Bangalore' then salary end) as Bangalore from employee group by
empid,empname order by empid;