VG Vaishali - SQL - Assessment
VG Vaishali - SQL - Assessment
INSTEAD DOWNLOAD THIS DOCUMENT TO YOUR LOCAL SYSTEM and RECORD YOUR RESPONSES IN THAT
FILE.
Q1) From the table above, find employees with their department names.
Ans – SELECT e.EmployeeID, e.Name, d.DepartmentName
FROM Employees e
JOIN Departments d
ON e.DepartmentID = d. DepartmentID;
Q2) From the table above, list all employees, including those without a department.
FROM Employees e
ON e.DepartmentID = d.DepartmentID;
Q3) From the table above, list all employees and departments, showing matches where possible.
FROM Employees e
Q4) From the table above, Generate all possible pairs of employees and projects.
FROM Employees e
Q5) From the table above, Find pairs of employees with the same department.
e1.DepartmentID
FROM Employees e1
JOIN Employees e2
ON e1.DepartmentID = e2.DepartmentID
Ans -
FROM Employees e
“2024-12-01
2024-12- 02
2024-12-03"
Write SQL queries (CREATE, INSERT, & SELECT, etc) to generate all possible combinations.
Ans ----
PROCESSOR_NAME VARCHAR(50));
RAM_SIZE VARCHAR(20));
STORAGE_SIZE VARCHAR(20));
-- INSERT INTO Processors (PROCESSORID, PROCESSOR_NAME) VALUES (1, 'Intel i5'), (2, 'Intel i7'), (3, 'AMD Ryzen 5');
-- RAM INSERT INTO RAM (RAMID, RAM_SIZE) VALUES (1, '8GB'), (2, '16GB');
--INSERT INTO STORAGE (STORAGEID, STORAGE_SIZE) VALUES (1, '256 GB SSD'), (2, '512 GB SSD'), (3, '1 TB SSD');
SELECT P.PROCESSOR_NAME, R.RAM_SIZE, S.STORAGE_SIZE FROM PROCESSORS P CROSS JOIN RAM R CROSS JOIN
STORAGE S;
Q9) Write SQL queries (CREATE, INSERT, SELECT), A retailer wants to identify combinations of products and stores
where a product isn’t available.
ANS ----
-- INSERT INTO STORES (STORE_ID, STORE_NAME) VALUES (1, 'STOREXYZ'), (2, 'STOREABX');
-- INSERT INTO INVENTORY (PRODUCT_ID, STORE_ID) VALUES ('ax890000869', 1), ('py562MM220', 2), ('00005233kh',
1), ('00005233kh', 2);
SELECT p.PRODUCT_ID, p.PRODUCT_NAME, s.STORE_ID, s.STORE_NAME FROM PRODUCTS p CROSS JOIN STORES s
LEFT JOIN INVENTORY i ON p.PRODUCT_ID = i.PRODUCT_ID AND s.STORE_ID = i.STORE_ID WHERE i.PRODUCT_ID IS
NULL;
Q10) Write a query to pair every product with every other product to analyze cross-sales opportunities.
SELECT p1.PRODUCT_NAME AS Product1, p2.PRODUCT_NAME AS Product2
FROM PRODUCTS p1
Q1) Based on the below reporting hierarchy, find their managers for each employee in an organization and the output
to be as follows:
ANS--
SELECT
Q2) Calculate a running total of sales for each product by date as per the below data
Ans ----
PARTITION BY ProductID
ORDER BY SaleDate
) AS RunningTotal
FROM SALES
Q1) Write a SQL query to parse full names into First Name, and Last Name
Customer
CUSTOMER
ID FULL_NAME
Alice
1 Johnson
2 Bob Smith
Charlie
3 Brown
Q2) Find Contacts with phone numbers not in the format of (XXX) XXX-XXXX
CONTACTS
CONTACTID PHONE_NUMBER
1 (123) 456-7890
2 1234567890
3 (987) 654-3210
Ans ---
Q3) Write a SQL query to convert email addresses to lowercase and remove spaces
EMAILs
EMAIL_ID EMAIL_ADDRESS
1 User@Examp le.com
2 [email protected]
Q4) Write a SQL query to find the current age based on their birthdates.
Q5) Write a query to find the missing dates from the below table.
Q6) Write a SQL query to round a set of prices to the nearest dollar and truncate to two decimals.
Q7) Write a SQL query to compute the standard deviation and variance for a set of sales based on the table below:
Q8) Write a SQL query to calculate rank and percentile ranks for the students below based on their scores.
Q9) Write a SQL query to extract the domain name from email addresses.
Q10) Write a SQL query to extract numeric values from mixed alphanumeric strings.
Q11) Extract first 3 characters from the below given product code
Q12) Write a query to replace all occurances of a substring in a text
SECTION4 - CASE STATEMENTS: 3Q’s
Q1) Calculate total sales for each region, but group "East" and "West" into "Domestic" and others into "International."
Ans ---
SELECT
CASE
WHEN REGION IN ('East', 'West') THEN 'Domestic'
ELSE 'International'
END AS RegionGroup,
SUM(AMOUNT) AS TotalSales
FROM SALES
GROUP BY RegionGroup;
ELSE 'D'
END AS GRADE
FROM GRADES;
Q3) Write a SQL query to apply a discount based on the product category
SELECT PRODUCT_ID, CATEGORY, PRICE,
CASE
END AS DISCOUNT
FROM PRODUCTS;
SECTION5 – Subqueries: 3Q’s
Q1) Write a query to find all customers who have spent more than the average purchase amount.
Ans -- SELECT CustomerID, PurchaseAmount
FROM PURCHASES
Q2)Find total sales by customer, add a column showing total sales for each customer
Ans --
FROM SALES
GROUP BY CustomerID;
Q3) Find top sales per customer, highest sales for each customer (refer to Q2 – SALES table of this section for your
reference)
GROUP BY CustomerID;
----------------------------------------------------------------------------
ANSWERS---------------------------------------------------------------------------------------