Data Analytics
111K subscribers
225 photos
1 video
2 files
952 links
Perfect channel to learn Data Analytics

Learn SQL, Python, Alteryx, Tableau, Power BI and many more

For Promotions: @coderfun @love_data
Download Telegram
๐Ÿณ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐—ง๐—ผ ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—œ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿ˜ 

โœ… 100% FREE & Beginner-Friendly
โœ… Learn AI, ML, Data Science, Ethical Hacking & More
โœ… Taught by Industry Experts
โœ… Practical & Hands-on Learning

๐Ÿ“ข Start learning today and take your tech career to the next level! ๐Ÿš€

๐‹๐ข๐ง๐ค ๐Ÿ‘‡:- 
 
https://pdlink.in/4bQ6FpS
 
Enroll For FREE & Get Certified ๐ŸŽ“
โค5
๐Ÿš€ Real-World SQL Scenario Based Interview Questions with Answers

๐Ÿ“Œ Question 1: Find Customers Who Purchased in Consecutive Months

Table: orders customer_id, order_date

Requirement: Identify customers who placed orders in consecutive months.

WITH monthly_orders AS (
SELECT DISTINCT
customer_id,
DATE_TRUNC('month', order_date) AS order_month
FROM orders
),
consecutive_orders AS (
SELECT
customer_id,
order_month,
LAG(order_month) OVER (
PARTITION BY customer_id
ORDER BY order_month
) AS prev_month
FROM monthly_orders
)
SELECT customer_id
FROM consecutive_orders
WHERE order_month = prev_month + INTERVAL '1 month';

๐Ÿ“Œ Question 2: Find the Top 3 Customers by Revenue Each Month
Table: orders customer_id, amount, order_date

WITH customer_revenue AS (
SELECT
DATE_TRUNC('month', order_date) AS month,
customer_id,
SUM(amount) AS revenue
FROM orders
GROUP BY 1, 2
)

SELECT *
FROM (
SELECT *,
DENSE_RANK() OVER (
PARTITION BY month
ORDER BY revenue DESC
) AS rnk
FROM customer_revenue
) t
WHERE rnk <= 3;

๐Ÿ“Œ Question 3: Calculate Running Total Revenue

Table: sales sale_date, amount
Requirement: Show cumulative revenue over time.

SELECT
sale_date,
amount,
SUM(amount) OVER (
ORDER BY sale_date
) AS running_revenue
FROM sales;

๐Ÿ“Œ Question 4: Find Users Who Have Not Logged In During the Last 30 Days
Tables: users user_id, logins user_id, login_date

SELECT u.user_id
FROM users u
LEFT JOIN logins l
ON u.user_id = l.user_id
GROUP BY u.user_id
HAVING MAX(login_date) < CURRENT_DATE - INTERVAL '30 days'
OR MAX(login_date) IS NULL;

๐Ÿ“Œ Question 5: Detect Duplicate Transactions

Table: transactions transaction_id, customer_id, amount, transaction_date
Requirement: Find duplicate transactions based on customer, amount, and date.

SELECT
customer_id,
amount,
transaction_date,
COUNT(*) AS duplicate_count
FROM transactions
GROUP BY customer_id, amount, transaction_date
HAVING COUNT(*) > 1;

๐Ÿ“Œ Question 6: Calculate Average Order Value by Month

Table: orders order_id, amount, order_date

SELECT
DATE_TRUNC('month', order_date) AS month,
ROUND(AVG(amount), 2) AS avg_order_value
FROM orders
GROUP BY DATE_TRUNC('month', order_date)
ORDER BY month;

๐Ÿ“Œ Question 7: Find the Most Recent Order for Each Customer
Table: orders order_id, customer_id, order_date

WITH ranked_orders AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) AS rn
FROM orders
)
SELECT
customer_id,
order_id,
order_date
FROM ranked_orders
WHERE rn = 1;

๐Ÿ“Œ Question 8: Calculate Product Contribution to Total Revenue

Table: sales product_id, amount
Requirement: Find percentage contribution of each product.

SELECT
product_id,
SUM(amount) AS revenue,
ROUND(
100.0 * SUM(amount) /
SUM(SUM(amount)) OVER (),
2
) AS contribution_pct
FROM sales
GROUP BY product_id;

๐Ÿ“Œ Question 9: Find Customers with No Orders
Tables: customers customer_id, orders customer_id

SELECT c.customer_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;

๐Ÿ“Œ Question 10: Calculate 7-Day Moving Average Sales

Table: sales sale_date, amount

SELECT
sale_date,
amount,
ROUND(
AVG(amount) OVER (
ORDER BY sale_date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
),
2
) AS moving_avg_7_days
FROM sales;

โค๏ธ Double Tap For More
โค20๐Ÿ‘2
๐Ÿ“Š ๐—ง๐—–๐—ฆ ๐—™๐—ฅ๐—˜๐—˜ ๐——๐—ฎ๐˜๐—ฎ ๐—”๐—ป๐—ฎ๐—น๐˜†๐˜๐—ถ๐—ฐ๐˜€ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€

Here's an amazing opportunity from TCS to learn essential data analytics skills completely FREE and earn a certificate

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlink.in/4waJYWJ

๐Ÿ”ฅ Data Analytics continues to be one of the most in-demand career paths, and this free course is a great first step toward building job-ready skills.

โณ Don't miss this opportunity to upskill and boost your career!
โค7
๐Ÿš€ SQL Scenario Based Interview Questions with Answers Part 2

๐Ÿ“Œ Question 11: Find the Second Highest Salary in Each Department 
Table: employees (employee_id, department_id, salary)

WITH ranked_salary AS (
    SELECT *,
           DENSE_RANK() OVER (
               PARTITION BY department_id
               ORDER BY salary DESC
           ) AS rnk
    FROM employees
)
SELECT department_id, employee_id, salary
FROM ranked_salary
WHERE rnk = 2;


๐Ÿ“Œ Question 12: Identify Users Who Purchased on Their First Visit 
Tables: visits (user_id, visit_date) | orders (user_id, order_date)

WITH first_visit AS (
    SELECT user_id,
           MIN(visit_date) AS first_visit_date
    FROM visits
    GROUP BY user_id
)
SELECT DISTINCT f.user_id
FROM first_visit f
JOIN orders o
    ON f.user_id = o.user_id
   AND f.first_visit_date = o.order_date;


๐Ÿ“Œ Question 13: Find Products Never Sold 
Tables: products (product_id, product_name) | sales (product_id)

SELECT p.product_id, p.product_name
FROM products p
LEFT JOIN sales s
       ON p.product_id = s.product_id
WHERE s.product_id IS NULL;


๐Ÿ“Œ Question 14: Calculate Month-over-Month Revenue Growth 
Table: orders (order_date, revenue)

WITH monthly_revenue AS (
    SELECT DATE_TRUNC('month', order_date) AS month,
           SUM(revenue) AS total_revenue
    FROM orders
    GROUP BY 1
)
SELECT month,
       total_revenue,
       LAG(total_revenue) OVER (ORDER BY month) AS previous_month,
       ROUND(
           100.0 *
           (total_revenue - LAG(total_revenue) OVER (ORDER BY month))
           /
           LAG(total_revenue) OVER (ORDER BY month),
           2
       ) AS growth_pct
FROM monthly_revenue;


๐Ÿ“Œ Question 15: Find Employees Earning More Than Department Average 
Table: employees (employee_id, department_id, salary)

SELECT employee_id, department_id, salary
FROM (
    SELECT *,
           AVG(salary) OVER (
               PARTITION BY department_id
           ) AS dept_avg
    FROM employees
) t
WHERE salary > dept_avg;


๐Ÿ“Œ Question 16: Find Longest Consecutive Login Streak 
Table: logins (user_id, login_date)

WITH cte AS (
    SELECT user_id,
           login_date,
           login_date -
           ROW_NUMBER() OVER (
               PARTITION BY user_id
               ORDER BY login_date
           ) * INTERVAL '1 day' AS grp
    FROM logins
)
SELECT user_id, COUNT(*) AS streak_days
FROM cte
GROUP BY user_id, grp
ORDER BY streak_days DESC;


๐Ÿ“Œ Question 17: Find Peak Sales Day of Every Month 
Table: sales (sale_date, amount)

WITH daily_sales AS (
    SELECT DATE(sale_date) AS sale_day,
           SUM(amount) AS revenue
    FROM sales
    GROUP BY DATE(sale_date)
)
SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER (
               PARTITION BY DATE_TRUNC('month', sale_day)
               ORDER BY revenue DESC
           ) rn
    FROM daily_sales
) t
WHERE rn = 1;


๐Ÿ“Œ Question 18: Find Customers Who Ordered Every Month 
Table: orders (customer_id, order_date)

WITH customer_months AS (
    SELECT customer_id,
           COUNT(DISTINCT DATE_TRUNC('month', order_date)) AS months_active
    FROM orders
    GROUP BY customer_id
),
total_months AS (
    SELECT COUNT(DISTINCT DATE_TRUNC('month', order_date)) AS total_months
    FROM orders
)
SELECT customer_id
FROM customer_months c
CROSS JOIN total_months t
WHERE c.months_active = t.total_months;


๐Ÿ“Œ Question 19: Find Top Selling Product Category 
Tables: products (product_id, category) | sales (product_id, quantity)

SELECT category, SUM(quantity) AS total_sold
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY category
ORDER BY total_sold DESC
LIMIT 1;


๐Ÿ“Œ Question 20: Calculate Median Salary 
Table: employees (employee_id, salary)

SELECT PERCENTILE_CONT(0.5)
WITHIN GROUP (ORDER BY salary)
AS median_salary
FROM employees;


๐Ÿ’ก Double Tap โค๏ธ For More
โค17
๐ŸŽ“ ๐—š๐—ผ๐—ผ๐—ด๐—น๐—ฒ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€

Learn job-ready skills from Google and boost your resume?๐ŸŒŸ

โœ”๏ธ Learn from Google Experts
โœ”๏ธ Industry-Recognized Certificates
โœ”๏ธ Beginner-Friendly Learning Paths
โœ”๏ธ Self-Paced Courses
โœ”๏ธ Enhance Resume & LinkedIn Profile
โœ”๏ธ Build Job-Ready Skills

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlink.in/4vjLGVq

โณ Start Learning Today & Upgrade Your Career!
โค7๐Ÿ‘Ž5
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find employees who earn more than the average salary of their own department.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
employee_id,
employee_name,
department,
salary
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department = e.department
);


๐Ÿ’ก Explanation:

The query uses a correlated subquery to calculate the average salary for each employee's department.

โ€ข The outer query iterates through each employee.
โ€ข The inner query calculates the average salary of that employee's department.
โ€ข If an employee's salary is greater than their department's average, they're included in the result.

This is a classic SQL interview question that tests your understanding of:
โœ… Correlated Subqueries
โœ… Aggregate Functions (AVG)
โœ… Filtering with WHERE

๐ŸŽฏ Expected Output Example


+----------+------------+--------+
| Employee | Department | Salary |
+----------+------------+--------+
| John | IT | 90,000 |
| Sarah | HR | 70,000 |
| David | Finance | 85,000 |
+----------+------------+--------+


(Only employees earning above their department's average salary.)

๐Ÿš€ Correlated subqueries are asked frequently in interviews. Learn when to use themโ€”and also know how to rewrite them using window functions for better performance on large datasets.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค22
๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐Ÿญ๐Ÿฌ๐Ÿฌ+ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐—ณ๐—ผ๐—ฟ ๐—”๐˜‡๐˜‚๐—ฟ๐—ฒ, ๐—”๐—œ, ๐—–๐˜†๐—ฏ๐—ฒ๐—ฟ๐˜€๐—ฒ๐—ฐ๐˜‚๐—ฟ๐—ถ๐˜๐˜† & ๐— ๐—ผ๐—ฟ๐—ฒ ๐Ÿš€

Learn the most in-demand tech skills from Microsoft completely FREE๐ŸŒŸ

Microsoft Learn offers 100+ free courses designed to help students, freshers, and professionals build job-ready skills in today's fastest-growing technology domains.

โœ… 100% Free Learning
โœ… Beginner to Advanced Levels

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlink.in/4f0GNuH

๐Ÿš€ Learn. Practice. Upskill. Get Career Ready
โค2๐Ÿ‘Ž1
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:

You have 2 minutes to solve this SQL query.

Find the employees who have the highest salary in each department.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
    employee_id,
    employee_name,
    department,
    salary
FROM (
    SELECT
        employee_id,
        employee_name,
        department,
        salary,
        DENSE_RANK() OVER (
            PARTITION BY department
            ORDER BY salary DESC
        ) AS rnk
    FROM employees
) ranked
WHERE rnk = 1;


๐Ÿ’ก Explanation:

This query uses the DENSE_RANK() window function to rank employees by salary within each department.

โ€ข PARTITION BY department creates separate rankings for each department.

โ€ข ORDER BY salary DESC ranks the highest salary as 1.

โ€ข DENSE_RANK() ensures that if multiple employees have the same highest salary, they all receive Rank 1.

โ€ข The outer query filters only the employees with rnk = 1.

This question tests your knowledge of:

โœ… Window Functions

โœ… DENSE_RANK() vs RANK() vs ROW_NUMBER()

โœ… Partitioning Data

๐ŸŽฏ Output Example

Employee | Department | Salary

John | IT | 95,000

Sarah | HR | 80,000

David | Finance | 90,000

Alice | IT | 95,000

(John and Alice both appear because they share the highest salary in the IT department.)

๐Ÿš€ Whenever an interview question asks for the top N records per group, think of window functions.

DENSE_RANK(), RANK(), and ROW_NUMBER() are among the most commonly tested SQL concepts.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค16
๐Ÿ“Š ๐—ฃ๐˜„๐—– ๐—ถ๐˜€ ๐—ผ๐—ณ๐—ณ๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด ๐—ฎ ๐—™๐—ฅ๐—˜๐—˜ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ

This helps tolearn data visualization, dashboard creation, KPI analysis, and business intelligence skills that companies actively look for.

โœ… Free Certificate
โœ… Self-Paced Learning
โœ… Hands-On Power BI Projects
โœ… Beginner Friendly
โœ… Resume & LinkedIn Boost

Don't miss this opportunity to add an in-demand skill to your profile and stand out from the crowd! ๐Ÿ’ผ๐Ÿ”ฅ

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlink.in/4g5sKFa

Share with yours friends who wants to start a career in Data Analytics
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find employees whose salary is higher than their manager's salary.

Assume the table structure is:

employees(employee_id, employee_name, manager_id, salary)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
e.employee_id,
e.employee_name,
e.salary AS employee_salary,
m.employee_name AS manager_name,
m.salary AS manager_salary
FROM employees e
JOIN employees m
ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;

๐Ÿ’ก Explanation:

This query uses a self join because both employees and managers are stored in the same table.

โ€ข e represents the employee.
โ€ข m represents the manager.
โ€ข The join matches each employee with their manager using manager_id.
โ€ข The WHERE clause filters employees whose salary is greater than their manager's salary.

This question tests your understanding of:
โœ… Self Joins
โœ… Aliases (e and m)
โœ… Comparing values across related rows

๐ŸŽฏ Expected Output Example

Employee Employee Salary Manager Manager Salary

John 90,000 David 80,000
Sarah 85,000 Michael 75,000


๐Ÿš€ Self joins are one of the most frequently asked SQL interview topics. Practice scenarios involving employees, managers, organizational hierarchies, categories, and parent-child relationships.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค17
๐Ÿ“Š ๐—™๐—ฅ๐—˜๐—˜ ๐—ง๐—ฎ๐˜๐—ฎ ๐——๐—ฎ๐˜๐—ฎ ๐—”๐—ป๐—ฎ๐—น๐˜†๐˜๐—ถ๐—ฐ๐˜€ ๐—ฉ๐—ถ๐—ฟ๐˜๐˜‚๐—ฎ๐—น ๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐—ป๐˜€๐—ต๐—ถ๐—ฝ | ๐—ช๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ ๐Ÿš€

Here's an amazing opportunity to complete the FREE Tata Data Analytics Virtual Internship and earn a certificate that you can showcase on your Resume and LinkedIn.

โœ… 100% FREE
โœ… Self-Paced & Online
โœ… Beginner-Friendly
โœ… Certificate on Completion
โœ… Real Business Case Studies
โœ… Resume & LinkedIn Boost

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlink.in/4eybW8J

๐Ÿš€ Upskill Today. Build Your Portfolio. Get Career Ready!
โค9
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:

You have 2 minutes to solve this SQL query.

Find the top 3 highest-paid employees in each department.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT

    employee_id,

    employee_name,

    department,

    salary

FROM (

    SELECT

        employee_id,

        employee_name,

        department,

        salary,

        DENSE_RANK() OVER (

            PARTITION BY department

            ORDER BY salary DESC

        ) AS salary_rank

    FROM employees

) ranked

WHERE salary_rank <= 3

ORDER BY department, salary DESC;

๐Ÿ’ก Explanation:

The query uses the DENSE_RANK() window function to rank employees based on salary within each department.

โ€ข PARTITION BY department creates a separate ranking for every department.

โ€ข ORDER BY salary DESC ranks the highest salary first.

โ€ข DENSE_RANK() assigns the same rank to employees with identical salaries.

โ€ข The outer query returns only employees with a rank of 3 or less.

This question tests your understanding of:

โœ… Window Functions

โœ… DENSE_RANK()

โœ… Top N per Group

โœ… Partitioning Data

๐ŸŽฏ Expected Output Example

Employee  Department Salary Rank

John  IT  95,000 1

Alice  IT  95,000 1

Bob  IT  90,000 2

Mike  IT  85,000 3

Sarah  HR  80,000 1

๐Ÿš€ Know when to use each ranking function:

โ€ข ROW_NUMBER() โ†’ No ties (unique ranking)

โ€ข RANK() โ†’ Leaves gaps after ties

โ€ข DENSE_RANK() โ†’ No gaps after ties (ideal for Top N with ties)

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค22
๐Ÿ“Š ๐—™๐—ฅ๐—˜๐—˜ ๐——๐—ฎ๐˜๐—ฎ ๐—”๐—ป๐—ฎ๐—น๐˜†๐˜๐—ถ๐—ฐ๐˜€ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ | ๐—ก๐—ผ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—ก๐—ฒ๐—ฒ๐—ฑ๐—ฒ๐—ฑ! ๐Ÿš€

Want to start a career in Data Analytics but don't know where to begin?

These 5 FREE beginner-friendly courses will help you learn the most in-demand data skills and build a strong foundation.

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlink.in/3SOk64h

๐Ÿš€ Start Learning Today. Build Your Portfolio. Land Your Dream Data Job!
โค1
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find customers who have never placed an order.

Tables:

customers(customer_id, customer_name)

orders(order_id, customer_id, order_date)


๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
c.customer_id,
c.customer_name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;

๐Ÿ’ก Explanation:

This query uses a LEFT JOIN to return all customers, whether or not they have placed an order.

โ€ข LEFT JOIN keeps every customer in the result.
โ€ข Customers without matching records in the orders table will have NULL values.
โ€ข The WHERE o.customer_id IS NULL condition filters only customers who have never placed an order.

This question tests your understanding of:
โœ… LEFT JOIN
โœ… Finding missing records
โœ… NULL handling

๐ŸŽฏ Expected Output Example

Customer ID Customer Name

105 Alice
112 David
118 Sarah


๐Ÿš€ Questions about finding unmatched records are very common in interviews. Practice using:

- LEFT JOIN ... IS NULL

- NOT EXISTS

- NOT IN (carefully, because NULL values can affect results)

Among these, NOT EXISTS is often preferred for correctness and performance in many databases.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค11๐Ÿ”ฅ1
๐—ง๐—–๐—ฆ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ข๐—ป ๐——๐—ฎ๐˜๐—ฎ ๐— ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ๐—บ๐—ฒ๐—ป๐˜ - ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ˜

TCS iON is offering a FREE Master Data Management Course with a Certificate,

โœ… 100% FREE Learning
โœ… Certificate on Completion
โœ… Self-Paced Online Course
โœ… Beginner-Friendly Content
โœ… Industry-Relevant Skills
โœ… Resume & LinkedIn Profile Boost

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlink.in/4jGFBw0

๐Ÿš€ Start Learning Today. Upskill for Free. Get Career Ready!
โค3๐Ÿ‘1๐Ÿฅฐ1
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find the department with the highest average salary.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
department,
AVG(salary) AS average_salary
FROM employees
GROUP BY department
ORDER BY average_salary DESC
LIMIT 1;

๐Ÿ’ก Explanation:
The query calculates the average salary for each department and returns the department with the highest average salary.

โ€ข GROUP BY department groups employees by department
โ€ข AVG(salary) calculates the average salary for each department
โ€ข ORDER BY average_salary DESC sorts departments from highest to lowest average salary
โ€ข LIMIT 1 returns only the top department

This question tests your understanding of:
โœ… GROUP BY
โœ… Aggregate Functions AVG
โœ… ORDER BY
โœ… LIMIT

๐ŸŽฏ Expected Output
Department Average_Salary
IT 88,500

๐Ÿš€ Bonus Handles Ties
If multiple departments share the highest average salary, use DENSE_RANK():

SELECT
department,
average_salary
FROM (
SELECT
department,
AVG(salary) AS average_salary,
DENSE_RANK() OVER (
ORDER BY AVG(salary) DESC
) AS rnk
FROM employees
GROUP BY department
) ranked
WHERE rnk = 1;

This version returns all departments tied for the highest average salary.

๐Ÿš€ Whenever you see questions like highest, lowest, top N, or rank, think beyond LIMIT. Ask yourself: What if there's a tie? Window functions like DENSE_RANK() often provide a more complete solution.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค15
๐Ÿš€ ๐—ก๐—ฉ๐—œ๐——๐—œ๐—” ๐—™๐—ฅ๐—˜๐—˜ ๐—”๐—œ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ | ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—™๐—ฟ๐—ผ๐—บ ๐—”๐—œ ๐—œ๐—ป๐—ฑ๐˜‚๐˜€๐˜๐—ฟ๐˜† ๐—Ÿ๐—ฒ๐—ฎ๐—ฑ๐—ฒ๐—ฟ๐˜€

Want to build cutting-edge *AI skills* from one of the world's leading AI and GPU companies?

*NVIDIA* offers *FREE AI Certification Courses* to help students, freshers, developers, and professionals

๐Ÿ”— ๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—™๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜๐Ÿ‘‡:

https://pdlinks.in/nvdia

๐Ÿš€ Start Learning Today. Earn Your Certificate. Build Your Future in AI!
โค1
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find employees who earn the same salary as at least one other employee in the same department.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช
SELECT
employee_id,
employee_name,
department,
salary
FROM employees
WHERE (department, salary) IN (
SELECT
department,
salary
FROM employees
GROUP BY department, salary
HAVING COUNT(*) > 1
)
ORDER BY department, salary DESC;

๐Ÿ’ก Explanation:
The query identifies duplicate salary values within each department.

โ€ข The subquery groups records by department and salary.
โ€ข **HAVING COUNT(*) > 1** finds salary values that appear more than once in the same department.
โ€ข The outer query returns all employees whose (department, salary) matches those duplicate combinations.

This question tests your understanding of:
โœ… GROUP BY
โœ… HAVING
โœ… Multi-column filtering
โœ… Identifying duplicate records

๐ŸŽฏ Expected Output Example
Employee Department Salary
John IT 80,000
Alice IT 80,000
David HR 65,000
Sarah HR 65,000

๐Ÿš€ Alternative Using Window Functions
SELECT
employee_id,
employee_name,
department,
salary
FROM (
SELECT
*,
COUNT(*) OVER (
PARTITION BY department, salary
) AS salary_count
FROM employees
) t
WHERE salary_count > 1;

This approach avoids a subquery with GROUP BY and is a great way to showcase your knowledge of window functions.

๐Ÿš€ When interview questions ask you to find duplicates, think of these three approaches:

1. GROUP BY + HAVING
2. Window functions COUNT() OVER
3. Self Join for specific comparison scenarios

Knowing multiple solutions demonstrates strong SQL problem-solving skills.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค11
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find the third highest salary from the employees table without using LIMIT or TOP.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
salary
FROM (
SELECT
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees
) ranked
WHERE salary_rank = 3;

๐Ÿ’ก Explanation:
This query uses the DENSE_RANK() window function to rank distinct salary values in descending order.

โ€ข ORDER BY salary DESC assigns Rank 1 to the highest salary.
โ€ข DENSE_RANK() ensures duplicate salaries receive the same rank.
โ€ข The outer query filters for salary_rank = 3, returning the third highest distinct salary.

๐ŸŽฏ Expected Output Example
Salary Rank
95,000 1
90,000 2
85,000 3

Output:
Third Highest Salary
85,000

๐Ÿš€ Alternative Solution Using a Correlated Subquery
SELECT DISTINCT salary
FROM employees e1
WHERE 2 = (
SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary > e1.salary
);

This solution avoids window functions and is commonly asked to test your understanding of correlated subqueries.

๐Ÿš€ Questions involving the Nth highest or Nth lowest value are interview favorites. Be comfortable solving them using:

1. DENSE_RANK()
2. RANK()
3. Correlated Subqueries
4. Common Table Expressions (CTEs)

Being able to provide multiple approaches leaves a strong impression on interviewers.

โค๏ธ React with โค๏ธ for more interview challenges!
โค17
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find the employees who joined in the last 30 days.

Assume the table structure:
employees(employee_id, employee_name, department, joining_date)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
employee_id,
employee_name,
department,
joining_date
FROM employees
WHERE joining_date >= CURRENT_DATE - INTERVAL '30 days';

๐Ÿ’ก Explanation:
This query filters employees whose joining date falls within the last 30 days.

โ€ข CURRENT_DATE returns today's date.
โ€ข INTERVAL '30 days' subtracts 30 days from the current date.
โ€ข The WHERE clause returns employees who joined on or after that date.

This question tests your understanding of:
โœ… Date Functions
โœ… Date Arithmetic
โœ… Filtering Records with Dates

๐ŸŽฏ Expected Output Example
Employee Department Joining Date
John IT 2026-06-10
Sarah HR 2026-06-22

๐Ÿš€ Database-Specific Alternatives

MySQL
SELECT *
FROM employees
WHERE joining_date >= CURDATE() - INTERVAL 30 DAY;

SQL Server
SELECT *
FROM employees
WHERE joining_date >= DATEADD(DAY, -30, GETDATE());

Oracle
SELECT *
FROM employees
WHERE joining_date >= SYSDATE - 30;

๐Ÿš€ Tip for SQL Job Seekers:
Date-related SQL questions are very common. Make sure you're comfortable with:
โ€ข Finding records from the last N days
โ€ข Current month/year filters
โ€ข Date differences
โ€ข Date formatting
โ€ข Database-specific date functions

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค19
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find the customer(s) who placed the highest number of orders.

Tables:
customers(customer_id, customer_name)
orders(order_id, customer_id, order_date)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
customer_id,
customer_name,
total_orders
FROM (
SELECT
c.customer_id,
c.customer_name,
COUNT(o.order_id) AS total_orders,
DENSE_RANK() OVER (
ORDER BY COUNT(o.order_id) DESC
) AS rnk
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id
GROUP BY
c.customer_id,
c.customer_name
) ranked
WHERE rnk = 1;


๐Ÿ’ก Explanation:
The query first counts the total number of orders placed by each customer and then ranks them based on the order count.

โœ… COUNT(o.order_id) calculates the number of orders per customer.

โœ… GROUP BY ensures one row per customer.

โœ… DENSE_RANK() ranks customers from highest to lowest order count.

โœ… The outer query returns all customers with rnk = 1, including ties.

This question tests your understanding of:

โœ… JOIN

โœ… GROUP BY

โœ… Aggregate Functions COUNT

โœ… Window Functions DENSE_RANK

๐ŸŽฏ Expected Output Example

+----------+--------------+
| Customer | Total Orders |
+----------+--------------+
| John | 25 |
| Sarah | 25 |
+----------+--------------+


Both customers are returned because they are tied for the highest number of orders.

๐Ÿš€ Tip for SQL Job Seekers:
Whenever an interview asks for the highest, lowest, most, or least, think about whether multiple records could tie for first place. Using DENSE_RANK() instead of LIMIT 1 makes your solution more robust and interview-ready.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
โค18