10 SQL Queries Every Fresher Data Analyst Should Practise
Almost every data analyst interview in Bangalore has a SQL round. The good news: most questions come from a small set of patterns. If you can write these ten queries from memory and explain them, you are ready for most fresher interviews.
Imagine two tables: customers(id, name, city) and orders(id, customer_id, amount, order_date).
1. Filter and sort
Orders above 5,000, newest first: SELECT * FROM orders WHERE amount > 5000 ORDER BY order_date DESC;
2. Count per group
Customers per city: SELECT city, COUNT(*) FROM customers GROUP BY city;
3. Filter groups with HAVING
Cities with more than 100 customers: add HAVING COUNT(*) > 100. Interviewers often ask how HAVING differs from WHERE: WHERE filters rows before grouping, HAVING filters groups after.
4. INNER JOIN
Each order with the customer's name: SELECT o.id, c.name, o.amount FROM orders o JOIN customers c ON c.id = o.customer_id;
5. LEFT JOIN to find missing data
Customers who never ordered: SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL;
6. Total and average per customer
SELECT customer_id, SUM(amount), AVG(amount) FROM orders GROUP BY customer_id;
7. Top N
Top 5 customers by spend: sort the query above by SUM(amount) DESC and add LIMIT 5.
8. Second highest value
A classic: SELECT MAX(amount) FROM orders WHERE amount < (SELECT MAX(amount) FROM orders);
9. Monthly totals
Group by month to show a trend: in MySQL, SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(amount) FROM orders GROUP BY month;
10. Window functions
Rank customers inside each city: RANK() OVER (PARTITION BY city ORDER BY total DESC). Window functions come up more and more in interviews, so practise at least RANK, ROW_NUMBER and a running total.
How to practise
Create these two tables in MySQL with 20 to 30 rows of made-up data, then write each query without looking. Say out loud what it does. That habit of explaining your query clearly is what interviewers remember.
If you would like a guided path with Excel, SQL, Power BI, Tableau and Python, plus mock interviews, see the data analyst course in Bangalore at ScholarLogic, BTM Layout.
Arjun is a counselor at ScholarLogic.
Comments
Post a Comment