SlideShare a Scribd company logo
TOP 10 SQL
INTERVIEW QUERIES
By Rishabh Mishra
2
Rishabh Mishra
EmpID EmpName Gender Salary City
1 Arjun M 75000 Pune
2 Ekadanta M 125000 Bangalore
3 Lalita F 150000 Mathura
4 Madhav M 250000 Delhi
5 Visakha F 120000 Mathura
Practice Dataset
Employee Table
EmployeeDetail Table
EmpID Project EmpPosition DOJ
1 P1 Executive 26-01-2019
2 P2 Executive 04-05-2020
3 P1 Lead 21-10-2021
4 P3 Manager 29-11-2018
5 P2 Manager 01-08-2020
CREATE TABLE Employee (
EmpID int NOT NULL,
EmpName Varchar,
Gender Char,
Salary int,
City Char(20) )
--- first run the above code then below code
INSERT INTO Employee
VALUES (1, 'Arjun', 'M', 75000, 'Pune'),
(2, 'Ekadanta', 'M', 125000, 'Bangalore'),
(3, 'Lalita', 'F', 150000 , 'Mathura'),
(4, 'Madhav', 'M', 250000 , 'Delhi'),
(5, 'Visakha', 'F', 120000 , 'Mathura')
3
Rishabh Mishra
CREATE TABLE EmployeeDetail (
EmpID int NOT NULL,
Project Varchar,
EmpPosition Char(20),
DOJ date )
--- first run the above code then below code
INSERT INTO EmployeeDetail
VALUES (1, 'P1', 'Executive', '26-01-2019'),
(2, 'P2', 'Executive', '04-05-2020'),
(3, 'P1', 'Lead', '21-10-2021'),
(4, 'P3', 'Manager', '29-11-2019'),
(5, 'P2', 'Manager', '01-08-2020')
Create Tables: Employee and EmployeeDetail
Q1(b): Write a query to retrieve the list of employees from the same city.
4
Rishabh Mishra
SELECT E1.EmpID, E1.EmpName, E1.City
FROM Employee E1, Employee E2
WHERE E1.City = E2.City AND E1.EmpID != E2.EmpID
Q1(a): Find the list of employees whose salary ranges between 2L to 3L.
SELECT EmpName, Salary FROM Employee
WHERE Salary > 200000 AND Salary < 300000
--- OR –--
SELECT EmpName, Salary FROM Employee
WHERE Salary BETWEEN 200000 AND 300000
SELECT * FROM Employee
WHERE EmpID IS NULL
Q1(c): Query to find the null values in the Employee table.
5
Rishabh Mishra
SELECT
(COUNT(*) FILTER (WHERE Gender = 'M') * 100.0 / COUNT(*)) AS MalePct,
(COUNT(*) FILTER (WHERE Gender = 'F') * 100.0 / COUNT(*)) AS FemalePct
FROM Employee;
Q2(b): What’s the male and female employees ratio.
Q2(a): Query to find the cumulative sum of employee’s salary.
SELECT EmpID, Salary, SUM(Salary) OVER (ORDER BY EmpID) AS CumulativeSum
FROM Employee
Q2(c): Write a query to fetch 50% records from the Employee table.
SELECT * FROM Employee
WHERE EmpID <= (SELECT COUNT(EmpID)/2 from Employee)
If EmpID is not auto-increment field or numeric, then we can use Row NUMBER function
6
Rishabh Mishra
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(Salary, LEN(Salary)-2), 'XX') as masked_salary
FROM Employee
SELECT Salary,
CONCAT(SUBSTRING(Salary::text, 1, LENGTH(Salary::text)-2), 'XX') as masked_number
FROM Employee
--- OR –--
SELECT Salary, CONCAT(LEFT(CAST(Salary AS text), LENGTH(CAST(Salary AS text))-2), 'XX')
AS masked_number
FROM Employee
MySQL
Q4: Write a query to fetch even and odd rows from Employee table.
7
Rishabh Mishra
If you have an auto-increment field
like EmpID then we can use the
MOD() function:
---Fetch even rows
SELECT * FROM Employee
WHERE MOD(EmpID,2)=0;
---Fetch odd rows
SELECT * FROM Employee
WHERE MOD(EmpID,2)=1;
---Fetch even rows
SELECT * FROM
(SELECT *, ROW_NUMBER() OVER(ORDER BY EmpId) AS
RowNumber
FROM Employee) AS Emp
WHERE Emp.RowNumber % 2 = 0
---Fetch odd rows
SELECT * FROM
(SELECT *, ROW_NUMBER() OVER(ORDER BY EmpId) AS
RowNumber
FROM Employee) AS Emp
WHERE Emp.RowNumber % 2 = 1
Alternative Solution
General Solution using ROW_NUMBER()
8
Rishabh Mishra
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’
SELECT * FROM Employee WHERE EmpName LIKE 'A%';
SELECT * FROM Employee WHERE EmpName LIKE '_a%';
SELECT * FROM Employee WHERE EmpName LIKE '%y_';
SELECT * FROM Employee WHERE EmpName LIKE '____l';
SELECT * FROM Employee WHERE EmpName LIKE 'V%a'
9
Rishabh Mishra
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
SELECT DISTINCT EmpName
FROM Employee
WHERE LOWER(EmpName) SIMILAR TO '[aeiou]%'
SELECT DISTINCT EmpName
FROM Employee
WHERE LOWER(EmpName) SIMILAR TO '%[aeiou]'
SELECT DISTINCT EmpName
FROM Employee
WHERE LOWER(EmpName) SIMILAR TO '[aeiou]%[aeiou]'
SELECT DISTINCT EmpName
FROM Employee
WHERE LOWER(EmpName) REGEXP '^[aeiou]'
SELECT DISTINCT EmpName
FROM Employee
WHERE LOWER(EmpName) REGEXP '[aeiou]$'
SELECT DISTINCT EmpName
FROM Employee
WHERE LOWER(EmpName) REGEXP
'^[aeiou].*[aeiou]$'
MySQL Solution: REGEXP
Q6: Find Nth highest salary from employee table with and without using the
TOP/LIMIT keywords.
10
Rishabh Mishra
SELECT Salary FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET N-1
SELECT Salary FROM Employee E1
WHERE N-1 = (
SELECT COUNT( DISTINCT ( E2.Salary ) )
FROM Employee E2
WHERE E2.Salary > E1.Salary );
--- OR ---
SELECT Salary FROM Employee E1
WHERE N = (
SELECT COUNT( DISTINCT ( E2.Salary ) )
FROM Employee E2
WHERE E2.Salary >= E1.Salary );
Using LIMIT
General Solution without using TOP/LIMIT
SELECT TOP 1 Salary
FROM Employee
WHERE Salary < (
SELECT MAX(Salary) FROM Employee)
AND Salary NOT IN (
SELECT TOP 2 Salary
FROM Employee
ORDER BY Salary DESC)
ORDER BY Salary DESC;
Using TOP
11
Rishabh Mishra
Q7(a): Write a query to find and remove duplicate records from a table.
SELECT EmpID, EmpName, gender, Salary, city,
COUNT(*) AS duplicate_count
FROM Employee
GROUP BY EmpID, EmpName, gender, Salary, city
HAVING COUNT(*) > 1;
Q7(b): Query to retrieve the list of employees working in same project.
WITH CTE AS
(SELECT e.EmpID, e.EmpName, ed.Project
FROM Employee AS e
INNER JOIN EmployeeDetail AS ed
ON e.EmpID = ed.EmpID)
SELECT c1.EmpName, c2.EmpName, c1.project
FROM CTE c1, CTE c2
WHERE c1.Project = c2.Project AND c1.EmpID != c2.EmpID AND c1.EmpID < c2.EmpID
DELETE FROM Employee
WHERE EmpID IN
(SELECT EmpID FROM Employee
GROUP BY EmpID
HAVING COUNT(*) > 1);
12
Rishabh Mishra
Q8: Show the employee with the highest salary for each project
SELECT ed.Project, MAX(e.Salary) AS ProjectSal
FROM Employee AS e
INNER JOIN EmployeeDetail AS ed
ON e.EmpID = ed.EmpID
GROUP BY Project
ORDER BY ProjectSal DESC;
WITH CTE AS
(SELECT project, EmpName, salary,
ROW_NUMBER() OVER (PARTITION BY project ORDER BY salary DESC) AS row_rank
FROM Employee AS e
INNER JOIN EmployeeDetail AS ed
ON e.EmpID = ed.EmpID)
SELECT project, EmpName, salary
FROM CTE
WHERE row_rank = 1;
Alternative, more dynamic solution: here you can fetch EmpName, 2nd/3rd highest value, etc
Similarly we can find Total Salary for each
project, just use SUM() instead of MAX()
13
Rishabh Mishra
Q9: Query to find the total count of employees joined each year
SELECT EXTRACT('year' FROM doj) AS JoinYear, COUNT(*) AS EmpCount
FROM Employee AS e
INNER JOIN EmployeeDetail AS ed ON e.EmpID = ed.EmpID
GROUP BY JoinYear
ORDER BY JoinYear ASC
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 EmpName, Salary,
CASE
WHEN Salary > 200000 THEN 'High'
WHEN Salary >= 100000 AND Salary <= 200000 THEN 'Medium'
ELSE 'Low'
END AS SalaryStatus
FROM Employee
14
Rishabh Mishra
BONUS: 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"
FROM Employee
GROUP BY EmpID, EmpName;
Instagram: https://www.instagram.com/rishabhnmishra/
LinkedIn: https://www.linkedin.com/in/rishabhnmishra/
YouTube: https://www.youtube.com/@RishabhMishraOfficial

More Related Content

PDF
Sql queries
DOCX
Ravi querys 425
DOCX
Sql Queries
DOCX
Complex queries in sql
PPTX
Nested and sub query aggregate functions and clauses ppt
PDF
Vishwajeet Sikhwal ,BCA,Final Year 2015
PDF
Vijay Kumar
DOCX
Mysql and html
Sql queries
Ravi querys 425
Sql Queries
Complex queries in sql
Nested and sub query aggregate functions and clauses ppt
Vishwajeet Sikhwal ,BCA,Final Year 2015
Vijay Kumar
Mysql and html

Similar to SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf (20)

DOCX
Sql file (1)
PDF
Pooja Bijawat,Bachelor Degree in Computer Application
PPT
Sub queries
DOCX
2 q on groups
PDF
Apurv Gupta, BCA ,Final year , Dezyne E'cole College
DOC
Sql task answers
PDF
Priyanka Bhatia.BCA Final year 2015
PDF
Simran kaur,BCA Final Year 2015
PDF
Sql queries questions and answers
DOC
21390228-SQL-Queries.doc
PDF
Orcl sql queries
PDF
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
PPT
SQL querys in detail || Sql query slides
PDF
Sql queries
PDF
Pooja Jain
PDF
Important dbms practical question
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PPTX
SQL case study Analysis PPT by Radhika Kashidd
PDF
DBMS Lab
Sql file (1)
Pooja Bijawat,Bachelor Degree in Computer Application
Sub queries
2 q on groups
Apurv Gupta, BCA ,Final year , Dezyne E'cole College
Sql task answers
Priyanka Bhatia.BCA Final year 2015
Simran kaur,BCA Final Year 2015
Sql queries questions and answers
21390228-SQL-Queries.doc
Orcl sql queries
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
SQL querys in detail || Sql query slides
Sql queries
Pooja Jain
Important dbms practical question
Database Systems - SQL - DDL Statements (Chapter 3/3)
SQL case study Analysis PPT by Radhika Kashidd
DBMS Lab
Ad

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
master seminar digital applications in india
PDF
RMMM.pdf make it easy to upload and study
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Weekly quiz Compilation Jan -July 25.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
VCE English Exam - Section C Student Revision Booklet
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
master seminar digital applications in india
RMMM.pdf make it easy to upload and study
Orientation - ARALprogram of Deped to the Parents.pptx
Computing-Curriculum for Schools in Ghana
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Weekly quiz Compilation Jan -July 25.pdf
Ad

SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf

  • 1. TOP 10 SQL INTERVIEW QUERIES By Rishabh Mishra
  • 2. 2 Rishabh Mishra EmpID EmpName Gender Salary City 1 Arjun M 75000 Pune 2 Ekadanta M 125000 Bangalore 3 Lalita F 150000 Mathura 4 Madhav M 250000 Delhi 5 Visakha F 120000 Mathura Practice Dataset Employee Table EmployeeDetail Table EmpID Project EmpPosition DOJ 1 P1 Executive 26-01-2019 2 P2 Executive 04-05-2020 3 P1 Lead 21-10-2021 4 P3 Manager 29-11-2018 5 P2 Manager 01-08-2020
  • 3. CREATE TABLE Employee ( EmpID int NOT NULL, EmpName Varchar, Gender Char, Salary int, City Char(20) ) --- first run the above code then below code INSERT INTO Employee VALUES (1, 'Arjun', 'M', 75000, 'Pune'), (2, 'Ekadanta', 'M', 125000, 'Bangalore'), (3, 'Lalita', 'F', 150000 , 'Mathura'), (4, 'Madhav', 'M', 250000 , 'Delhi'), (5, 'Visakha', 'F', 120000 , 'Mathura') 3 Rishabh Mishra CREATE TABLE EmployeeDetail ( EmpID int NOT NULL, Project Varchar, EmpPosition Char(20), DOJ date ) --- first run the above code then below code INSERT INTO EmployeeDetail VALUES (1, 'P1', 'Executive', '26-01-2019'), (2, 'P2', 'Executive', '04-05-2020'), (3, 'P1', 'Lead', '21-10-2021'), (4, 'P3', 'Manager', '29-11-2019'), (5, 'P2', 'Manager', '01-08-2020') Create Tables: Employee and EmployeeDetail
  • 4. Q1(b): Write a query to retrieve the list of employees from the same city. 4 Rishabh Mishra SELECT E1.EmpID, E1.EmpName, E1.City FROM Employee E1, Employee E2 WHERE E1.City = E2.City AND E1.EmpID != E2.EmpID Q1(a): Find the list of employees whose salary ranges between 2L to 3L. SELECT EmpName, Salary FROM Employee WHERE Salary > 200000 AND Salary < 300000 --- OR –-- SELECT EmpName, Salary FROM Employee WHERE Salary BETWEEN 200000 AND 300000 SELECT * FROM Employee WHERE EmpID IS NULL Q1(c): Query to find the null values in the Employee table.
  • 5. 5 Rishabh Mishra SELECT (COUNT(*) FILTER (WHERE Gender = 'M') * 100.0 / COUNT(*)) AS MalePct, (COUNT(*) FILTER (WHERE Gender = 'F') * 100.0 / COUNT(*)) AS FemalePct FROM Employee; Q2(b): What’s the male and female employees ratio. Q2(a): Query to find the cumulative sum of employee’s salary. SELECT EmpID, Salary, SUM(Salary) OVER (ORDER BY EmpID) AS CumulativeSum FROM Employee Q2(c): Write a query to fetch 50% records from the Employee table. SELECT * FROM Employee WHERE EmpID <= (SELECT COUNT(EmpID)/2 from Employee) If EmpID is not auto-increment field or numeric, then we can use Row NUMBER function
  • 6. 6 Rishabh Mishra 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(Salary, LEN(Salary)-2), 'XX') as masked_salary FROM Employee SELECT Salary, CONCAT(SUBSTRING(Salary::text, 1, LENGTH(Salary::text)-2), 'XX') as masked_number FROM Employee --- OR –-- SELECT Salary, CONCAT(LEFT(CAST(Salary AS text), LENGTH(CAST(Salary AS text))-2), 'XX') AS masked_number FROM Employee MySQL
  • 7. Q4: Write a query to fetch even and odd rows from Employee table. 7 Rishabh Mishra If you have an auto-increment field like EmpID then we can use the MOD() function: ---Fetch even rows SELECT * FROM Employee WHERE MOD(EmpID,2)=0; ---Fetch odd rows SELECT * FROM Employee WHERE MOD(EmpID,2)=1; ---Fetch even rows SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY EmpId) AS RowNumber FROM Employee) AS Emp WHERE Emp.RowNumber % 2 = 0 ---Fetch odd rows SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY EmpId) AS RowNumber FROM Employee) AS Emp WHERE Emp.RowNumber % 2 = 1 Alternative Solution General Solution using ROW_NUMBER()
  • 8. 8 Rishabh Mishra 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’ SELECT * FROM Employee WHERE EmpName LIKE 'A%'; SELECT * FROM Employee WHERE EmpName LIKE '_a%'; SELECT * FROM Employee WHERE EmpName LIKE '%y_'; SELECT * FROM Employee WHERE EmpName LIKE '____l'; SELECT * FROM Employee WHERE EmpName LIKE 'V%a'
  • 9. 9 Rishabh Mishra 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 SELECT DISTINCT EmpName FROM Employee WHERE LOWER(EmpName) SIMILAR TO '[aeiou]%' SELECT DISTINCT EmpName FROM Employee WHERE LOWER(EmpName) SIMILAR TO '%[aeiou]' SELECT DISTINCT EmpName FROM Employee WHERE LOWER(EmpName) SIMILAR TO '[aeiou]%[aeiou]' SELECT DISTINCT EmpName FROM Employee WHERE LOWER(EmpName) REGEXP '^[aeiou]' SELECT DISTINCT EmpName FROM Employee WHERE LOWER(EmpName) REGEXP '[aeiou]$' SELECT DISTINCT EmpName FROM Employee WHERE LOWER(EmpName) REGEXP '^[aeiou].*[aeiou]$' MySQL Solution: REGEXP
  • 10. Q6: Find Nth highest salary from employee table with and without using the TOP/LIMIT keywords. 10 Rishabh Mishra SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET N-1 SELECT Salary FROM Employee E1 WHERE N-1 = ( SELECT COUNT( DISTINCT ( E2.Salary ) ) FROM Employee E2 WHERE E2.Salary > E1.Salary ); --- OR --- SELECT Salary FROM Employee E1 WHERE N = ( SELECT COUNT( DISTINCT ( E2.Salary ) ) FROM Employee E2 WHERE E2.Salary >= E1.Salary ); Using LIMIT General Solution without using TOP/LIMIT SELECT TOP 1 Salary FROM Employee WHERE Salary < ( SELECT MAX(Salary) FROM Employee) AND Salary NOT IN ( SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC) ORDER BY Salary DESC; Using TOP
  • 11. 11 Rishabh Mishra Q7(a): Write a query to find and remove duplicate records from a table. SELECT EmpID, EmpName, gender, Salary, city, COUNT(*) AS duplicate_count FROM Employee GROUP BY EmpID, EmpName, gender, Salary, city HAVING COUNT(*) > 1; Q7(b): Query to retrieve the list of employees working in same project. WITH CTE AS (SELECT e.EmpID, e.EmpName, ed.Project FROM Employee AS e INNER JOIN EmployeeDetail AS ed ON e.EmpID = ed.EmpID) SELECT c1.EmpName, c2.EmpName, c1.project FROM CTE c1, CTE c2 WHERE c1.Project = c2.Project AND c1.EmpID != c2.EmpID AND c1.EmpID < c2.EmpID DELETE FROM Employee WHERE EmpID IN (SELECT EmpID FROM Employee GROUP BY EmpID HAVING COUNT(*) > 1);
  • 12. 12 Rishabh Mishra Q8: Show the employee with the highest salary for each project SELECT ed.Project, MAX(e.Salary) AS ProjectSal FROM Employee AS e INNER JOIN EmployeeDetail AS ed ON e.EmpID = ed.EmpID GROUP BY Project ORDER BY ProjectSal DESC; WITH CTE AS (SELECT project, EmpName, salary, ROW_NUMBER() OVER (PARTITION BY project ORDER BY salary DESC) AS row_rank FROM Employee AS e INNER JOIN EmployeeDetail AS ed ON e.EmpID = ed.EmpID) SELECT project, EmpName, salary FROM CTE WHERE row_rank = 1; Alternative, more dynamic solution: here you can fetch EmpName, 2nd/3rd highest value, etc Similarly we can find Total Salary for each project, just use SUM() instead of MAX()
  • 13. 13 Rishabh Mishra Q9: Query to find the total count of employees joined each year SELECT EXTRACT('year' FROM doj) AS JoinYear, COUNT(*) AS EmpCount FROM Employee AS e INNER JOIN EmployeeDetail AS ed ON e.EmpID = ed.EmpID GROUP BY JoinYear ORDER BY JoinYear ASC 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 EmpName, Salary, CASE WHEN Salary > 200000 THEN 'High' WHEN Salary >= 100000 AND Salary <= 200000 THEN 'Medium' ELSE 'Low' END AS SalaryStatus FROM Employee
  • 14. 14 Rishabh Mishra BONUS: 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" FROM Employee GROUP BY EmpID, EmpName;