sql_interview
sql_interview
Follow me Here:
LinkedIn:
https://www.linkedin.com/in/ajay026/
Data Geeks Community:
https://lnkd.in/gU5NkCqi
SQL
INTERVIEW QUESTIONS
KIT FOR
DATA ENGINEERS
Table of Contents:
1. Why SQL
2. Significance of SQL for Interviews.
3. SQL Roadmap
4. Top SQL Commands
5. 5 SQL Projects you can practice.
6. 150+ SQL Interview questions (Scenario based)
7. Best Practices for SQL
8. FREE Resources.
Calculating Percentiles
Question: How would you write a query to find the 90th
percentile of sales from a Sales table?
Solution:
SELECT PERCENTILE_CONT(0.90) WITHIN GROUP (ORDER BY
sales_amount) AS p90_sales
FROM Sales;
Explanation:
This uses the PERCENTILE_CONT function to calculate the 90th
percentile of the sales.
BEGIN TRANSACTION;
-- Perform the update or insert operation
COMMIT;
Explanation:
Setting the transaction isolation level to SERIALIZABLE
ensures that transactions are executed in a way that prevents
concurrency issues.
How would you delete rows that are duplicates but keep
the first occurrence?
Answer: Use ROW_NUMBER() with a CTE to assign row
numbers and delete duplicates.
Solution:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY column
ORDER BY column) AS row_num
FROM table
Explain the GROUP BY clause and when you would use it.
Answer: GROUP BY groups rows sharing a common field, used
with aggregate functions like SUM, COUNT, AVG.
Explain what a cross join is and when you might use it.
Answer: A cross join returns the Cartesian product of two
tables. It’s rarely used unless you need all possible
combinations of two sets.
How would you select records in one table that don’t exist
in another table?
Answer: Use LEFT JOIN or NOT EXISTS.
Solution:
SELECT a.*
FROM table_a a
LEFT JOIN table_b b ON a.id = b.id
WHERE b.id IS NULL;
Given transaction data, how would you find the day with
the highest transactions?
How would you find the average sales by month and year?
Answer: Group by both YEAR and MONTH functions.
Solution:
SELECT YEAR(sale_date) AS year, MONTH(sale_date) AS
month, AVG(sales) AS avg_sales
FROM sales
GROUP BY year, month;
How would you get the running total of sales over time?
Answer: Use SUM with OVER for cumulative totals.
Solution:
SELECT sale_date, sales,
SUM(sales) OVER (ORDER BY sale_date) AS running_total
FROM sales;
My Interview Experience
How would you find employees who joined within the last
quarter?
How would you identify users who placed their last order
in December last year?
Answer:
Solution:
SELECT customer_id
FROM orders
How would you calculate the total sales for each product
category by year?
Answer:
Solution:
SELECT category, YEAR(sale_date) AS year, SUM(sales) AS
total_sales
FROM products
JOIN sales ON products.product_id = sales.product_id
GROUP BY category, year;
How would you find the average order amount for the top
10% of orders?
Answer: Use NTILE() to rank orders into percentiles.
Solution:
WITH percentiles AS (
SELECT order_id, amount, NTILE(10) OVER (ORDER BY
amount DESC) AS percentile
FROM orders
)
SELECT AVG(amount) AS top_10_percent_avg
FROM percentiles
WHERE percentile = 1;
How do you find orders that took longer than the average
processing time?
How would you find products that haven’t been sold in the
last six months?
Answer:
Solution:
SELECT product_id
FROM products
LEFT JOIN sales ON products.product_id = sales.product_id
WHERE sales.sale_date < DATEADD(MONTH, -6, GETDATE())
OR sales.product_id IS NULL;
How would you retrieve the highest daily sales for each
month?
Answer:
Solution:
How would you find the most popular time slots for orders
in a day?
Answer:
Solution:
SELECT HOUR(order_time) AS hour, COUNT(*) AS order_count
FROM orders
In Order to help you Please Find SQL RoadMap here for FREE
with resources…..
https://www.linkedin.com/posts/ajay026_sqloptimizations-
activity-7045023960769466368-
VFKZ?utm_source=share&utm_medium=member_desktop