Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

PRACTISE 1

CREATE TABLE Employee (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(255),
emp_salary DECIMAL(10, 2),
emp_department VARCHAR(50),
emp_hire_date DATE
);

INSERT INTO Employee (emp_id, emp_name, emp_salary, emp_department, emp_hire_date)


VALUES
(1, 'John Doe', 60000, 'IT', '2022-01-15'),
(2, 'Jane Smith', 75000, 'HR', '2021-08-22'),
(3, 'Bob Johnson', 90000, 'Finance', '2023-02-10'),
(4, 'Alice Lee', 80000, 'Marketing', '2022-11-05'),
(5, 'Chris Brown', 70000, 'IT', '2023-04-18');

Question 1:
Write a query to display the emp_name and a column named Salary_Status that categorizes
employees into 'High Salary' if their salary is above 80000, 'Medium Salary' if between 60000 and
80000 (inclusive), and 'Low Salary' otherwise.

Question 2:
Write a query to find the emp_name and emp_department for employees hired before 2022,
categorizing them into 'Before 2022' and '2022 and After' based on their hire date.

Question 3:
Write a query to display emp_name and emp_salary, and a column Bonus that gives a 10% bonus to
employees in the 'IT' department and 5% bonus to employees in the 'Finance' department, and 0%
bonus otherwise.

Question 4:
Write a query to list emp_name and emp_salary, and a column Tenure that categorizes employees
into 'New' if hired in 2023, 'Experienced' if hired in 2022, and 'Veteran' otherwise.

Question 5:
Write a query to display emp_name and emp_department, and a column Seniority that categorizes
employees into 'Senior' if their salary is above 80000 and they are in the 'IT' department,
'Intermediate' if their salary is above 70000, and 'Junior' otherwise.
Question 1:
Write a query to display the emp_name and a column named Salary_Status that categorizes
employees into 'High Salary' if their salary is above 80000, 'Medium Salary' if between 60000 and
80000 (inclusive), and 'Low Salary' otherwise.

Answer 1:

WITH SalaryCategories AS (
SELECT
emp_id,
emp_name,
emp_salary,
CASE
WHEN emp_salary > 80000 THEN 'High Salary'
WHEN emp_salary BETWEEN 60000 AND 80000 THEN 'Medium Salary'
ELSE 'Low Salary'
END AS Salary_Status
FROM
Employee
)

SELECT
emp_name,
Salary_Status
FROM
SalaryCategories;
Question 2:
Write a query to find the emp_name and emp_department for employees hired before 2022,
categorizing them into 'Before 2022' and '2022 and After' based on their hire date.

Answer 2:

WITH HireDateCategories AS (
SELECT
emp_id,
emp_name,
emp_department,
emp_hire_date,
CASE
WHEN emp_hire_date < '2022-01-01' THEN 'Before 2022'
ELSE '2022 and After'
END AS Hire_Date_Category
FROM
Employee
)

SELECT
emp_name,
emp_department,
Hire_Date_Category
FROM
HireDateCategories;
Question 3:
Write a query to display emp_name and emp_salary, and a column Bonus that gives a 10% bonus to
employees in the 'IT' department and 5% bonus to employees in the 'Finance' department, and 0%
bonus otherwise.

Answer 3:

WITH BonusCategories AS (
SELECT
emp_id,
emp_name,
emp_salary,
emp_department,
CASE
WHEN emp_department = 'IT' THEN emp_salary * 0.1
WHEN emp_department = 'Finance' THEN emp_salary * 0.05
ELSE 0
END AS Bonus
FROM
Employee
)

SELECT
emp_name,
emp_salary,
Bonus
FROM
BonusCategories;
Question 4:
Write a query to list emp_name and emp_salary, and a column Tenure that categorizes employees
into 'New' if hired in 2023, 'Experienced' if hired in 2022, and 'Veteran' otherwise.

Answer 4:

WITH TenureCategories AS (
SELECT
emp_id,
emp_name,
emp_salary,
CASE
WHEN EXTRACT(YEAR FROM emp_hire_date) = 2023 THEN 'New'
WHEN EXTRACT(YEAR FROM emp_hire_date) = 2022 THEN 'Experienced'
ELSE 'Veteran'
END AS Tenure
FROM
Employee
)

SELECT
emp_name,
emp_salary,
Tenure
FROM
TenureCategories;
Question 5:
Write a query to display emp_name and emp_department, and a column Seniority that categorizes
employees into 'Senior' if their salary is above 80000 and they are in the 'IT' department,
'Intermediate' if their salary is above 70000, and 'Junior' otherwise.

Answer 5:

WITH SeniorityCategories AS (
SELECT
emp_id,
emp_name,
emp_department,
CASE
WHEN emp_department = 'IT' AND emp_salary > 80000 THEN 'Senior'
WHEN emp_salary > 70000 THEN 'Intermediate'
ELSE 'Junior'
END AS Seniority
FROM
Employee
)

SELECT
emp_name,
emp_department,
Seniority
FROM
SeniorityCategories;
PRACTISE 2
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
total_amount DECIMAL(10, 2)
);

INSERT INTO Orders (order_id, customer_id, order_date, total_amount)


VALUES
(1, 101, '2022-01-15', 500.00),
(2, 102, '2021-08-22', 750.00),
(3, 103, '2023-02-10', 1200.00),
(4, 104, '2022-11-05', 800.00),
(5, 105, '2023-04-18', 600.00);

Question 1:
Write a query to display the order_id, order_date, and a column named Order_Status that
categorizes orders into 'High Value' if the total amount is above 1000, 'Medium Value' if between
500 and 1000 (inclusive), and 'Low Value' otherwise.

Question 2:
Write a query to find the customer_id, order_date, and a column Customer_Tier that categorizes
customers into 'VIP' if they have placed orders with a total amount above 1000, 'Regular' if between
500 and 1000, and 'Basic' otherwise.

Question 3:
Write a query to display order_id, order_date, and a column Discount that gives a 20% discount to
orders placed in 2023, 10% discount to orders placed in 2022, and 0% discount otherwise.

Question 4:
Write a query to list order_id, order_date, and a column Delivery_Status that categorizes orders into
'Delivered' if the order date is before the current date, 'Pending' if the order date is in the future,
and 'In Progress' otherwise.

Question 5:
Write a query to display order_id, order_date, and a column Payment_Method that categorizes
orders into 'Credit Card' if the customer_id is even, 'PayPal' if the customer_id is odd, and 'Other'
otherwise.
Question 1:
Write a query to display the order_id, order_date, and a column named Order_Status that
categorizes orders into 'High Value' if the total amount is above 1000, 'Medium Value' if between
500 and 1000 (inclusive), and 'Low Value' otherwise.

Answer 1:

WITH OrderStatusCategories AS (
SELECT
order_id,
order_date,
total_amount,
CASE
WHEN total_amount > 1000 THEN 'High Value'
WHEN total_amount BETWEEN 500 AND 1000 THEN 'Medium Value'
ELSE 'Low Value'
END AS Order_Status
FROM
Orders
)

SELECT
order_id,
order_date,
Order_Status
FROM
OrderStatusCategories;
Question 2:
Write a query to find the customer_id, order_date, and a column Customer_Tier that categorizes
customers into 'VIP' if they have placed orders with a total amount above 1000, 'Regular' if between
500 and 1000, and 'Basic' otherwise.

Answer 2:

WITH CustomerTierCategories AS (
SELECT
customer_id,
order_date,
total_amount,
CASE
WHEN total_amount > 1000 THEN 'VIP'
WHEN total_amount BETWEEN 500 AND 1000 THEN 'Regular'
ELSE 'Basic'
END AS Customer_Tier
FROM
Orders
)

SELECT
customer_id,
order_date,
Customer_Tier
FROM
CustomerTierCategories;
Question 3:
Write a query to display order_id, order_date, and a column Discount that gives a 20% discount to
orders placed in 2023, 10% discount to orders placed in 2022, and 0% discount otherwise.

Answer 3:

WITH DiscountCategories AS (
SELECT
order_id,
order_date,
CASE
WHEN EXTRACT(YEAR FROM order_date) = 2023 THEN total_amount * 0.2
WHEN EXTRACT(YEAR FROM order_date) = 2022 THEN total_amount * 0.1
ELSE 0
END AS Discount
FROM
Orders
)

SELECT
order_id,
order_date,
Discount
FROM
DiscountCategories;
Question 4:
Write a query to list order_id, order_date, and a column Delivery_Status that categorizes orders into
'Delivered' if the order date is before the current date, 'Pending' if the order date is in the future,
and 'In Progress' otherwise.

Answer 4:

WITH DeliveryStatusCategories AS (
SELECT
order_id,
order_date,
CASE
WHEN order_date < CURRENT_DATE THEN 'Delivered'
WHEN order_date > CURRENT_DATE THEN 'Pending'
ELSE 'In Progress'
END AS Delivery_Status
FROM
Orders
)

SELECT
order_id,
order_date,
Delivery_Status
FROM
DeliveryStatusCategories;
Question 5:
Write a query to display order_id, order_date, and a column Payment_Method that categorizes
orders into 'Credit Card' if the customer_id is even, 'PayPal' if the customer_id is odd, and 'Other'
otherwise.

Answer 5:

WITH PaymentMethodCategories AS (
SELECT
order_id,
order_date,
customer_id,
CASE
WHEN customer_id % 2 = 0 THEN 'Credit Card'
WHEN customer_id % 2 = 1 THEN 'PayPal'
ELSE 'Other'
END AS Payment_Method
FROM
Orders
)

SELECT
order_id,
order_date,
Payment_Method
FROM
PaymentMethodCategories;

You might also like