Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways

10 Most Asked SQL Interview Questions

10 Most Asked SQL Interview Questions will help you understand the type of questions asked in SQL interviews in the Analytics domain.
Below are the 10 Most Asked SQL Interview Questions.
Here are a few FREE and paid resources that will help you in making career in Analytics

The Data Monk – We have 2000+ solved interview questions, you can explore all of these for free

The Data Monk Youtube channel
We have ~200 Videos created only based on the most asked topics in SQL, Python, ML, Statistics, Case Stud,ies and other topics. Do subscribe and watch 1-2 video daily.

Link to Channel – The Data Monk

The Data Monk E-book

Full Stack Analytics Professional – Full Stack Analytics Professional – 2200 Most Asked Interview Questions
Data Analyst – 1100+ Most Asked Data Analyst Interview Questions
Business Analyst – 1250+ Most Asked Business Analyst Interview Questions
Data Scientist – 23 e-books covering all the ML Algorithms Interview Questions

The Data Monk Instagram Channel – Where we regularly post about the most asked definitions and questions


Question 1: Suppose you have a database with two tables:

  1. Customers
    • Columns: CustomerID (Primary Key), FirstName, LastName, Email
  2. Orders
    • Columns: OrderID (Primary Key), CustomerID (Foreign Key referencing Customers), OrderDate, TotalAmount

Write an SQL query to retrieve the names and total amounts spent by customers who have made at least two orders, along with the number of orders they’ve made.

Answer 1:
SELECT
    c.FirstName,
    c.LastName,
    COUNT(o.CustomerID) AS NumberOfOrders,
    SUM(o.TotalAmount) AS TotalAmountSpent
FROM
    Customers c
JOIN
    Orders o ON c.CustomerID = o.CustomerID
GROUP BY
    c.CustomerID
HAVING
    COUNT(o.CustomerID) >= 2;

Question 2: Consider a table named Products:

  • Columns: ProductID (Primary Key), ProductName, Category, Price

Write an SQL query to find the average price of products in each category, along with the total number of products in that category.

Answer 2:
SELECT
    Category,
    AVG(Price) AS AveragePrice,
    COUNT(*) AS TotalProducts
FROM
    Products
GROUP BY
    Category;

Question 3: You have a table named Employees:

  • Columns: EmployeeID (Primary Key), FirstName, LastName, Department, Salary

Write an SQL query to find the top 5 earning employees in each department.

Answer 3:
SELECT
    e1.Department,
    e1.FirstName,
    e1.LastName,
    e1.Salary
FROM
    Employees e1
WHERE
    5 > (
        SELECT COUNT(DISTINCT e2.Salary)
        FROM Employees e2
        WHERE e2.Department = e1.Department AND e2.Salary > e1.Salary
    );

Question 4 :
Suppose you have a table named Employees with columns: EmployeeID, FirstName, LastName, and Department. Write an SQL query to retrieve the employees who have the same first names and are in the same department.

Answer 4:
SELECT e1.EmployeeID, e1.FirstName, e1.LastName, e1.Department
FROM Employees e1
JOIN Employees e2 ON e1.FirstName = e2.FirstName AND e1.EmployeeID <> e2.EmployeeID
                  AND e1.Department = e2.Department;


Question 5:
Given a table named Orders with columns: OrderID, OrderDate, and TotalAmount, write an SQL query to find the date with the highest total amount of orders.

Answer 5:
SELECT OrderDate, SUM(TotalAmount) AS Total 
FROM Orders 
GROUP BY OrderDate 
ORDER BY Total DESC 
LIMIT 1;

Question 6:
Consider a table named Books with columns: BookID, Title, AuthorID, and PublicationYear, and a table named Authors with columns: AuthorID, FirstName, and LastName. Write an SQL query to retrieve the top 3 authors with the highest average publication years.

Answer 6:
SELECT a.AuthorID, a.FirstName, a.LastName, AVG(b.PublicationYear) AS AvgPublicationYear 
FROM Authors a 
JOIN Books b ON a.AuthorID = b.AuthorID 
GROUP BY a.AuthorID, a.FirstName, a.LastName 
ORDER BY AvgPublicationYear DESC 
LIMIT 3;

Question 7:
Suppose you have a table named Transactions with columns: TransactionID, UserID, TransactionDate, and Amount. Write an SQL query to find the UserID of the user who made the highest total amount of transactions.

Answer 7:
SELECT UserID 
FROM Transactions 
GROUP BY UserID 
ORDER BY SUM(Amount) 
DESC LIMIT 1;

Question 8:
Given a table named Logs with columns: LogID, UserID, and Activity, write an SQL query to find the UserID that has performed all possible activities.

Answer 8:
SELECT UserID 
FROM Logs 
GROUP BY UserID 
HAVING COUNT(DISTINCT Activity) = 
(
   SELECT COUNT(DISTINCT Activity) FROM Logs
);

Question 9: What are the Constraints in SQL?

Constraints are used to specify the rules concerning data in the table. It can be applied for single or multiple fields in an SQL table during the creation of the table or after creating using the ALTER TABLE command. The constraints are:
NOT NULL – Restricts NULL value from being inserted into a column.
CHECK – Verifies that all values in a field satisfy a condition.
DEFAULT – Automatically assigns a default value if no value has been specified for the field.
UNIQUE – Ensures unique values are inserted into the field.
INDEX – Indexes a field providing faster retrieval of records.
PRIMARY KEY – Uniquely identifies each record in a table.
FOREIGN KEY – Ensures referential integrity for a record in another table.

Question10: How do the SQL commands flow at the back end?

Order of execution for an SQL query

1) FROM, including JOINs
2) WHERE
3) GROUP BY
4) HAVING
5) WINDOW Functions
6) SELECT
7) DISTINCT
8) UNION
9) ORDER BY
10) LIMIT AND OFFSET

Case Study for Analytics Interview

Case Study Statement – Suppose you work for Amazon, suggest top 20 KPIs that you should have on a CXO level dashboard

Sales and Revenue Metrics:

  1. Total Revenue: The total amount of money generated from all sales on the platform.
  2. Gross Merchandise Volume (GMV): The total value of goods sold through the platform, including Amazon’s and third-party sellers’ sales.
  3. Average Order Value (AOV): The average amount spent by customers in each order.
  4. Conversion Rate: The percentage of website visitors who make a purchase.
  5. Customer Lifetime Value (CLTV): The predicted total value a customer will generate over their lifetime as an Amazon customer.

Operational Metrics:

6. Order Fulfillment Rate: The percentage of orders fulfilled on time and accurately.

  1. Inventory Turnover: The rate at which inventory is sold and replaced within a specific period.
  2. Fulfillment Center Utilization: The capacity utilization of Amazon’s fulfillment centers.
  3. Return Rate: The percentage of orders returned by customers.

Customer Experience Metrics:

10. Customer Satisfaction (CSAT): Customer satisfaction rating based on post-purchase surveys.

  1. Net Promoter Score (NPS): A measure of customer loyalty and willingness to recommend Amazon to others.
  2. Customer Retention Rate: The percentage of customers who make repeat purchases.

Marketplace Metrics:

13. Number of Sellers: The total number of third-party sellers on Amazon’s marketplace.

  1. Third-Party Seller Sales: The percentage of overall sales generated by third-party sellers.
  2. Seller Feedback Rating: Ratings and feedback given by customers to third-party sellers.

Digital Metrics:

16. Amazon Web Services (AWS) Revenue: Revenue generated from Amazon’s cloud computing platform.

  1. Amazon Prime Membership Growth: The growth rate of Amazon Prime subscriptions.
  2. Digital Content Consumption: Metrics related to e-books, video streaming, and other digital content.

Logistics and Delivery Metrics:
19. Delivery Speed: Average time taken to deliver products to customers.

  1. Fulfillment Costs: Costs associated with warehousing, packaging, and shipping.

About TheDataMonkGrand Master

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)

Follow Me