SQL Queries for Interview
Q1. Given a sales_data table, write a query to display the month-over-year change in total sales.
Table structure:
CREATE TABLE sales_data (
Sale_id VARCHAR PRIMARY KEY,
Product_id VARCHAR,
Sale_amount DECIMAL(10,2),
Sale_date DATE
);
Required: Write an SQL query to calculate the percentage change in total sales for each month compared to the same month in the previous year.
Q2. You have a customer_orders table that tracks every order placed by customers. Find customers who placed orders in the first quarter of 2022 but did not place any orders in the first quarter of 2023.
Table structure:
CREATE TABLE customer_orders (
Order_id VARCHAR PRIMARY KEY,
Customer_id VARCHAR,
Order_amount DECIMAL(10,2),
Order_date DATE
);
Required: Write an SQL query to select the Customer_id of those who were active in Q1 of 2022 but inactive in Q1 of 2023.
Q3. Given an employee_tasks table, rank employees based on the number of tasks completed this month.
Table structure:
CREATE TABLE employee_tasks (
Task_id VARCHAR PRIMARY KEY,
Employee_id VARCHAR,
Task_status VARCHAR, -- Values can be 'Completed', 'In Progress', or 'Not Started'
Task_date DATE
);
Required: Write an SQL query to select Employee_id and a rank based on the count of completed tasks for the current month, with 1 being the employee with the most tasks completed.
Q4. Using the product_inventory table, identify products that have consistently low stock (below 20 units) for the last 5 days.
Table structure:
CREATE TABLE product_inventory (
Product_id VARCHAR,
Stock_quantity INT,
Record_date DATE
);
Required: Write an SQL query to select Product_id for products that had a stock quantity of less than 20 units for each of the last 5 recorded days.
Table structure:
CREATE TABLE customer_orders (
Order_id VARCHAR PRIMARY KEY,
Customer_id VARCHAR,
Order_amount DECIMAL(10,2),
Order_date DATE
);
Required: Write an SQL query to select the Customer_id of those who were active in Q1 of 2022 but inactive in Q1 of 2023.
Q3. Given an employee_tasks table, rank employees based on the number of tasks completed this month.
Table structure:
CREATE TABLE employee_tasks (
Task_id VARCHAR PRIMARY KEY,
Employee_id VARCHAR,
Task_status VARCHAR, -- Values can be 'Completed', 'In Progress', or 'Not Started'
Task_date DATE
);
Required: Write an SQL query to select Employee_id and a rank based on the count of completed tasks for the current month, with 1 being the employee with the most tasks completed.
Q4. Using the product_inventory table, identify products that have consistently low stock (below 20 units) for the last 5 days.
Table structure:
CREATE TABLE product_inventory (
Product_id VARCHAR,
Stock_quantity INT,
Record_date DATE
);
Required: Write an SQL query to select Product_id for products that had a stock quantity of less than 20 units for each of the last 5 recorded days.
Comments
Post a Comment