TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2927 4.37K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ: 
You have 2 minutes to solve this SQL query.

Find the product(s) that have never been ordered.

Tables: 
products(product_id, product_name) 
order_details(order_id, product_id, quantity)

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช 
SELECT 
    p.product_id, 
    p.product_name 
FROM products p 
LEFT JOIN order_details od 
    ON p.product_id = od.product_id 
WHERE od.product_id IS NULL;

๐Ÿ’ก Explanation: 
This query finds all products that do not have a matching record in the order_details table.

LEFT JOIN returns all products, regardless of whether they've been ordered. Products without matching orders will have NULL values for columns from order_details. WHERE od.product_id IS NULL filters only those products that have never been ordered.

This question tests your understanding of: LEFT JOIN, NULL handling, Finding unmatched records

๐ŸŽฏ Expected Output Example 
Product ID   Product Name 
104   Wireless Mouse 
118   USB Hub 
125   Laptop Stand 

๐Ÿš€ Alternative Using NOT EXISTS 
SELECT 
    p.product_id, 
    p.product_name 
FROM products p 
WHERE NOT EXISTS ( 
    SELECT 1 
    FROM order_details od 
    WHERE od.product_id = p.product_id 
);

NOT EXISTS is often preferred because it handles NULL values correctly and can perform better than other approaches in many database systems.

๐Ÿš€ Tip for SQL Job Seekers: 
Whenever you're asked to find records that don't exist in another table, consider these approaches: 

LEFT JOIN ... IS NULL, NOT EXISTS โœ… (often the best choice), NOT IN (be cautious with NULL values)

Knowing the pros and cons of each approach is a common interview discussion point.

โค๏ธ React with โค๏ธ for more SQL interview challenges!
  • โค 8
More from @sqlspecialist
  1. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  2. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
  3. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 5 Guys, let's continue our Data Analyst Interviewโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026๐Ÿ”Ÿ What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns frโ€ฆ
  6. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 4 Guys, let's continue our Data Analyst Interviewโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’