Technical Blog

Topic 1 SQL Practice

SQL Interview.

Order of execution in SQL.

FROM - WHERE - GROUP BY - HAVING - SELECT - ORDER BY - LIMIT.

ClauseFunction
FROMChoose ad join table to get the base data.
WHEREFilter the base data.
GROUP BYAggregate the base data.
HAVINGFilters the aggregated data.
SELECTReturns the final data.
ORDER BYSort the final data.
LIMITLimit the returned data to a row count.

SELECT category, AVG(sales) AS avg_sales FROM SalesData WHERE year>2020 GROUP BY category HAVING COUNT(*)>10 ORDER BY avg_sales DESC LIMIT 3;

CategoryAvg_sales
Electronics128
Utensils91
Books89

First it will take the FROM then condition and group and condition and order.

Find monthly sales and sort in desc order.

Order_dateSales
2021-01-0120
2021-01-0232
2021-02-0845
2021-02-0431
2021-03-2133
2021-04-0619
2021-04-0721
2021-04-2210

Output

YearsMonthsTotalSales
2021Feb76
2021Jan52
2021Mar52
2021Apr31

SELECT YEAR(Order_date) AS Years, MONTH(Order_date) AS Months, SUM(Sales) AS TotalSales FROM Products GROUP BY YEAR(Order_date), MONTH(Order_date) ORDER BY TotalSales DESC;

Find the candidate who is proficient in Python, SQL, PowerBi. Write the query to list the candidate who possess all of the required skills for the job. Sort the output by candidate Id in asc order.

Candidate_idSkills
101PowerBi
101Python
101SQL
102SQL
108Python
108PowerBi
108SQL

Output

Candidate_idSkill_count
1013
1083

SELECT Candidate_id, COUNT(skills) FROM Applications WHERE skills IN (“Python”,“SQL”,“Power Bi”) GROUP BY(Candidate_id) HAVING COUNT(skills)=3 ORDER BY (Candidate_id) ASC;

What is a Trigger in SQL.

Normalization.

What is Truncate, Delete, Drop statement.

What are rank, dense_rank and row_number.

What are clustered and non clustered index in SQL.