0% found this document useful (0 votes)
0 views7 pages

SQL Assessment

The document contains a series of SQL queries addressing various data retrieval tasks, including finding top and bottom salary earners by gender, calculating department salaries, and removing duplicates from tables. It also includes queries for analyzing employee login/logout times, calculating return percentages for orders, and pivoting sales data by category. Each query is structured to extract specific insights from the database, demonstrating advanced SQL techniques.

Uploaded by

kakkar.ankur.4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views7 pages

SQL Assessment

The document contains a series of SQL queries addressing various data retrieval tasks, including finding top and bottom salary earners by gender, calculating department salaries, and removing duplicates from tables. It also includes queries for analyzing employee login/logout times, calculating return percentages for orders, and pivoting sales data by category. Each query is structured to extract specific insights from the database, demonstrating advanced SQL techniques.

Uploaded by

kakkar.ankur.4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1: Find top two female employees with highest salaries and bottom two male employees with

lowest salaries, displaying Employee ID, Name, Gender, Salary.

WITH RankedEmployees AS (

SELECT

EmployeeID,

EmployeeName,

Gender,

Salary,

CASE

WHEN Gender = 'Female' THEN

RANK() OVER (PARTITION BY Gender ORDER BY Salary DESC)

WHEN Gender = 'Male' THEN

RANK() OVER (PARTITION BY Gender ORDER BY Salary ASC)

END AS SalaryRank

FROM Employee

SELECT

EmployeeID,

EmployeeName,

Gender,

Salary

FROM RankedEmployees

WHERE (Gender = 'Female' AND SalaryRank <= 2)

OR (Gender = 'Male' AND SalaryRank <= 2);


Q2: Departments where total salary > average salary (with Unit, Department, Salary, Employees
count)

SELECT

f.UnitID,

f.DepartmentID,

d.UnitName,

d.DepartmentName,

SUM(f.Salary) AS TotalSalary,

COUNT(f.EmployeeID) AS NumberOfEmployees

FROM FactTable f

JOIN DimensionTable d ON f.UnitID = d.UnitID AND f.DepartmentID = d.DepartmentID

GROUP BY f.UnitID, f.DepartmentID, d.UnitName, d.DepartmentName

HAVING SUM(f.Salary) > (SELECT AVG(Salary) FROM FactTable);

Q3: Top 2 and bottom 2 salary receivers from each Unit (Only males in IT unit)

WITH RankedSalaries AS (

SELECT

f.UnitID,

d.UnitName,

f.EmployeeID,

f.EmployeeName,

f.Gender,

f.Age,

f.Salary,

f.Department,

CASE

WHEN f.UnitID = 'U02' AND f.Gender != 'Male' THEN NULL

ELSE ROW_NUMBER() OVER (PARTITION BY f.UnitID ORDER BY Salary DESC)


END AS TopRank,

CASE

WHEN f.UnitID = 'U02' AND f.Gender != 'Male' THEN NULL

ELSE ROW_NUMBER() OVER (PARTITION BY f.UnitID ORDER BY Salary ASC)

END AS BottomRank

FROM FactTable f

JOIN DimensionTable d ON f.UnitID = d.UnitID

SELECT

UnitID,

UnitName,

EmployeeID,

EmployeeName,

Gender,

Age,

Salary,

Department,

CASE

WHEN TopRank <= 2 THEN 'Top 2'

WHEN BottomRank <= 2 THEN 'Bottom 2'

END AS Category

FROM RankedSalaries

WHERE (TopRank <= 2 OR BottomRank <= 2);

Q4: Employees earning more than their department's average salary

WITH DeptAvgSalary AS (

SELECT DepartmentID, AVG(Salary) AS AvgSalary


FROM Emp

GROUP BY DepartmentID

SELECT e.*

FROM Emp e

JOIN DeptAvgSalary d ON e.DepartmentID = d.DepartmentID

WHERE e.Salary > d.AvgSalary;

Q5: Country-wise percentage of returned orders

SELECT

a.Country,

COUNT(r.OrderID) * 100.0 / COUNT(a.OrderID) AS ReturnPercentage

FROM All_Orders a

LEFT JOIN Returned_Orders r ON a.OrderID = r.OrderID

GROUP BY a.Country;

Q6: Remove redundancy in cars table (model_name, color, brand)

sql

CopyEdit

SELECT DISTINCT model_name, color, brand, model_id

FROM (

SELECT

model_name,

color,

brand,

MIN(model_id) AS model_id

FROM cars
GROUP BY model_name, color, brand

) AS unique_cars;

Q7: Derive "actual_distance" in car_travels table

SELECT

car_id,

travel_date,

MAX(meter_reading) - MIN(meter_reading) AS actual_distance

FROM car_travels

GROUP BY car_id, travel_date;

Q8: Remove duplicate source-destination distances (keep A -> B only)

SELECT

MIN(source) AS Source,

MIN(destination) AS Destination,

MIN(distance) AS Distance

FROM (

SELECT

CASE WHEN source < destination THEN source ELSE destination END AS source,

CASE WHEN source < destination THEN destination ELSE source END AS destination,

distance

FROM src_dest_distance

) AS t

GROUP BY source, destination;

Q9: First Login and Last Logout for employees for last 7 days

SELECT
EmployeeID,

CAST(LoginDateTime AS DATE) AS LoginDate,

MIN(LoginDateTime) AS FirstLogin,

MAX(LogoutDateTime) AS LastLogout

FROM Emp

WHERE LoginDateTime >= DATEADD(DAY, -7, CAST(GETDATE() AS DATE))

GROUP BY EmployeeID, CAST(LoginDateTime AS DATE);

Q10: Fetch duplicate records from Emp table

SELECT

EmployeeID,

EmployeeName,

DepartmentID,

Salary,

COUNT(*) AS DuplicateCount

FROM Emp

GROUP BY EmployeeID, EmployeeName, DepartmentID, Salary

HAVING COUNT(*) > 1;

Q11: List team and opponent combinations

SELECT

t1.TeamName AS Team,

t2.TeamName AS Opponent

FROM teams t1

JOIN teams t2 ON t1.TeamID < t2.TeamID;

Q12: Return records based on total_count in travel_items table


SELECT

item_name

FROM travel_items

CROSS APPLY (

SELECT TOP (total_count) 1

FROM master.dbo.spt_values

) AS X;

(Note: master.dbo.spt_values is a trick to generate rows in SQL Server.)

Q13: Pivot CountryCategorySales table into separate category columns

SELECT

Country,

SUM(CASE WHEN Category = 'Furniture' THEN Sales ELSE 0 END) AS Furniture,

SUM(CASE WHEN Category = 'Office Supplies' THEN Sales ELSE 0 END) AS OfficeSupplies,

SUM(CASE WHEN Category = 'Technology' THEN Sales ELSE 0 END) AS Technology

FROM CountryCategorySales

GROUP BY Country;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy