ultimate SQL Queries for Interview

TOP 10 SQL Queries for Interview :

We’ll be using the following sample tables:

  1. Sales:
  • Columns: sale_id, sale_date, product_id, quantity, unit_price, total_amount, customer_id
  • Description: This table stores the details of each sale transaction.

2.Products:

  • Columns: product_id, product_name, category, price
  • Description: Contains details about each product.

3.Customers:

  • Columns: customer_id, customer_name, city, state
  • Description: Holds information about customers.

4.Salesperson:

  • Columns: salesperson_id, salesperson_name, region
  • Description: Information about the salespeople handling different regions.

Sample Data for Queries:

Sales Table:

sale_idsale_dateproduct_idquantityunit_pricetotal_amountcustomer_id
12024-01-1510152001000201
22024-02-101023150450202

Products Table:

product_idproduct_namecategoryprice
101LaptopElectronics800
102MouseAccessories50

Customers Table:

customer_idcustomer_namecitystate
201John DoeSeattleWA
202Jane SmithAustinTX

Salesperson Table:

JOINS
Mastering Oracle SQL Joins: The Ultimate Guide to Data Retrieval and Performance
salesperson_idsalesperson_nameregion
301Mike JohnsonNorth
302Sarah LeeSouth

Question 1: Identify the top 3 products that have generated the highest total sales amount across all transactions.

Answer:

SELECT 
    p.product_name,
    SUM(s.total_amount) AS total_sales
FROM 
    Sales s
JOIN 
    Products p ON s.product_id = p.product_id
GROUP BY 
    p.product_name
ORDER BY 
    total_sales DESC
LIMIT 3;

Optimization Suggestion: Ensure there’s an index on product_id in both Sales and Products tables for faster JOIN operations.


Question 2: Find the sales made by customers who purchased products from more than one category.

Answer:

SELECT 
    s.customer_id,
    c.customer_name,
    COUNT(DISTINCT p.category) AS category_count,
    SUM(s.total_amount) AS total_spent
FROM 
    Sales s
JOIN 
    Products p ON s.product_id = p.product_id
JOIN 
    Customers c ON s.customer_id = c.customer_id
GROUP BY 
    s.customer_id, c.customer_name
HAVING 
    COUNT(DISTINCT p.category) > 1;

Optimization Suggestion: Use composite indexes on (product_id, category) in the Products table.


Question 3: Find the month with the highest sales for each product category.

Answer:

SELECT 
    p.category,
    EXTRACT(MONTH FROM s.sale_date) AS sale_month,
    SUM(s.total_amount) AS monthly_sales
FROM 
    Sales s
JOIN 
    Products p ON s.product_id = p.product_id
GROUP BY 
    p.category, EXTRACT(MONTH FROM s.sale_date)
ORDER BY 
    p.category, monthly_sales DESC;

Optimization Suggestion: Consider using a date-based partition index on sale_date to speed up monthly calculations.


Question 4: Calculate the average sales amount per customer per product category.

Answer:

SELECT 
    s.customer_id,
    c.customer_name,
    p.category,
    AVG(s.total_amount) AS avg_sales
FROM 
    Sales s
JOIN 
    Products p ON s.product_id = p.product_id
JOIN 
    Customers c ON s.customer_id = c.customer_id
GROUP BY 
    s.customer_id, c.customer_name, p.category;

Question 5: Find sales people who have never sold products in the ‘Electronics’ category.

Answer:

ORACLE
Mastering Oracle Database Architecture: A Comprehensive Overview
SELECT 
    sp.salesperson_name
FROM 
    Salesperson sp
LEFT JOIN 
    Sales s ON sp.salesperson_id = s.salesperson_id
LEFT JOIN 
    Products p ON s.product_id = p.product_id
WHERE 
    p.category = 'Electronics'
    OR p.category IS NULL
GROUP BY 
    sp.salesperson_name
HAVING 
    COUNT(p.product_id) = 0;

Question 6: Retrieve the top 5 customers who have spent the most in total, regardless of the product category.

Answer:

SELECT 
    c.customer_name,
    SUM(s.total_amount) AS total_spent
FROM 
    Sales s
JOIN 
    Customers c ON s.customer_id = c.customer_id
GROUP BY 
    c.customer_name
ORDER BY 
    total_spent DESC
LIMIT 5;

Question 7: Identify the products that haven’t been sold in the last 6 months.

Answer:

SELECT 
    p.product_name
FROM 
    Products p
LEFT JOIN 
    Sales s ON p.product_id = s.product_id 
           AND s.sale_date >= CURRENT_DATE - INTERVAL '6 months'
WHERE 
    s.product_id IS NULL;

Question 8: Find the highest single sale transaction amount and the corresponding customer details.

Answer:

SELECT 
    s.sale_id,
    c.customer_name,
    s.total_amount
FROM 
    Sales s
JOIN 
    Customers c ON s.customer_id = c.customer_id
ORDER BY 
    s.total_amount DESC
LIMIT 1;

Question 9: Display the percentage contribution of each product to the overall sales.

Answer:

SELECT 
    p.product_name,
    (SUM(s.total_amount) * 100.0 / (SELECT SUM(total_amount) FROM Sales)) AS percentage_contribution
FROM 
    Sales s
JOIN 
    Products p ON s.product_id = p.product_id
GROUP BY 
    p.product_name;

Question 10: Retrieve the sales data for customers whose names start with the same letter as the cities they live in.

Answer:

SELECT 
    c.customer_name,
    c.city,
    SUM(s.total_amount) AS total_spent
FROM 
    Sales s
JOIN 
    Customers c ON s.customer_id = c.customer_id
WHERE 
    LEFT(c.customer_name, 1) = LEFT(c.city, 1)
GROUP BY 
    c.customer_name, c.city;

Note : These SQL queries are pretty versatile and work well with several popular databases. They run perfectly in PostgreSQL without any changes. If you’re using MySQL or MariaDB, you’ll just need to tweak things a bit, like swapping CURRENT_DATE with CURDATE(). For SQL Server, simple adjustments like using GETDATE() instead of CURRENT_DATE and DATEADD() for intervals will do the trick. Oracle is quite similar, but you’ll want to replace CURRENT_DATE with SYSDATE and use TO_CHAR() for date handling. SQLite is mostly compatible too, but make sure functions like EXTRACT() work as expected. In short, these queries are quite adaptable and should run smoothly with just a few tweaks, no matter which database you’re using!

An experienced PostgreSQL DBA/SQL developer with years of expertise in designing and implementing robust database solutions. Proficient in writing complex queries, developing stored procedures, and conducting performance tuning to deliver highly efficient data-driven applications that meet business requirements. If any queries feel free to contact us at ganeshpudi12@gmail.com

1 thought on “ultimate SQL Queries for Interview”

Leave a Reply

Home
SQL
PLSQL
Interview Q&A