0% found this document useful (0 votes)
0 views25 pages

VG Vaishali - SQL - Assessment

The document contains a series of SQL questions and answers, organized into sections covering joins, common table expressions, functions, case statements, and subqueries. Each section includes multiple queries addressing specific tasks such as retrieving employee information, generating combinations of products, and calculating sales data. The document serves as a resource for practicing SQL query writing and understanding various SQL concepts.

Uploaded by

vgvaishali2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views25 pages

VG Vaishali - SQL - Assessment

The document contains a series of SQL questions and answers, organized into sections covering joins, common table expressions, functions, case statements, and subqueries. Each section includes multiple queries addressing specific tasks such as retrieving employee information, generating combinations of products, and calculating sales data. The document serves as a resource for practicing SQL query writing and understanding various SQL concepts.

Uploaded by

vgvaishali2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

DO NOT RECORD YOUR RESPONSES HERE IN THIS DOCUMENT.

INSTEAD DOWNLOAD THIS DOCUMENT TO YOUR LOCAL SYSTEM and RECORD YOUR RESPONSES IN THAT
FILE.

SECTION1 – JOINS: 10 Q’s

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.

Ans - SELECT e.EmployeeID, e.Name, d.DepartmentName

FROM Employees e

LEFT JOIN Departments d

ON e.DepartmentID = d.DepartmentID;

Q3) From the table above, list all employees and departments, showing matches where possible.

Ans - SELECT e.EmployeeID, e.Name, d.DepartmentName

FROM Employees e

FULL OUTER JOIN Departments d


ON e.DepartmentID = d.DepartmentID;

Q4) From the table above, Generate all possible pairs of employees and projects.

Ans - SELECT e.EmployeeID, e.Name, p.ProjectID, p.ProjectName

FROM Employees e

CROSS JOIN Projects p;

Q5) From the table above, Find pairs of employees with the same department.

Ans - SELECT e1.EmployeeID AS Emp1ID, e1.Name AS Emp1Name,

e2.EmployeeID AS Emp2ID, e2.Name AS Emp2Name,

e1.DepartmentID

FROM Employees e1

JOIN Employees e2

ON e1.DepartmentID = e2.DepartmentID

WHERE e1.EmployeeID < e2.EmployeeID;


Q6) From the table above, write a query to find the employees who are either not assigned to a department or work in
a department that has no associated projects.

Ans -

SELECT e.EmployeeID, e.Name

FROM Employees e

LEFT JOIN Departments d ON e.DepartmentID = d.DepartmentID

LEFT JOIN Projects p ON d.DepartmentID = p.DepartmentID

WHERE e.DepartmentID IS NULL OR p.ProjectID IS NULL;


Q7) Generate a list of all dates in December 2024 (HINT: Data to be created and use the appropriate join types (INNER,
LEFT, RIGHT, FULL, CROSS) to get the output something like the below:

“2024-12-01

2024-12- 02

2024-12-03"

Ans -- --- unable to solve


Q8) A company sells laptops with different configurations of processors, RAM, and storage as per the below.

Write SQL queries (CREATE, INSERT, & SELECT, etc) to generate all possible combinations.

Ans ----

CREATE TABLE PROCESSORS (

PROCESSORID INT PRIMARY KEY,

PROCESSOR_NAME VARCHAR(50));

CREATE TABLE RAM (

RAMID INT PRIMARY KEY,

RAM_SIZE VARCHAR(20));

CREATE TABLE STORAGE(


STORAGEID INT PRIMARY KEY,

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 ----

CREATE TABLE PRODUCTS

( PRODUCT_ID VARCHAR(20) PRIMARY KEY, PRODUCT_NAME VARCHAR(50) );

CREATE TABLE STORES

( STORE_ID INT PRIMARY KEY, STORE_NAME VARCHAR(50) );

CREATE TABLE INVENTORY

( PRODUCT_ID VARCHAR(20), STORE_ID INT,

PRIMARY KEY (PRODUCT_ID, STORE_ID),

FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(PRODUCT_ID),

FOREIGN KEY (STORE_ID) REFERENCES STORES(STORE_ID) );

-- INSERT INTO PRODUCTS (PRODUCT_ID, PRODUCT_NAME) VALUES ('ax890000869', 'LAPTOP'), ('py562MM220',


'TABLET'), ('00005233kh', 'SMARTPHONE');

-- 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

CROSS JOIN PRODUCTS p2

WHERE p1.PRODUCT_ID < p2.PRODUCT_ID;

SECTION2 - Common Table Expressions (CTE): 2 Q’s

Q1) Based on the below reporting hierarchy, find their managers for each employee in an organization and the output
to be as follows:
ANS--

WITH RECURSIVE EmployeeHierarchy AS (

SELECT
Q2) Calculate a running total of sales for each product by date as per the below data

Ans ----

SELECT ProductID,SaleDate, Amount, SUM(Amount)


OVER (

PARTITION BY ProductID

ORDER BY SaleDate

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

) AS RunningTotal

FROM SALES

ORDER BY ProductID, SaleDate;


SECTION3 - Functions (String, Date & Time, Numeric, Statistical): 12 Q’s

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

Ans --- need to work on logic

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;

Q2) Assign grades based on student scores as per the below.

SELECT STUDENT_ID, SCORE,


CASE

WHEN SCORE >= 90 THEN 'A'

WHEN SCORE >= 80 THEN 'B'

WHEN SCORE >= 70 THEN 'C'

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

WHEN CATEGORY = 'ELECTRONICS' THEN PRICE * 0.10

WHEN CATEGORY = 'FURNITURE' THEN PRICE * 0.15

ELSE PRICE * 0.59

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

WHERE PurchaseAmount > (

SELECT AVG(PurchaseAmount) FROM PURCHASES;

Q2)Find total sales by customer, add a column showing total sales for each customer

Ans --

SELECT CustomerID, SUM(SalesAmount) AS TotalSales

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)

Ans -- SELECT CustomerID,MAX(SalesAmount) AS TopSale


FROM SALES

GROUP BY CustomerID;

----------------------------------------------------------------------------
ANSWERS---------------------------------------------------------------------------------------

You might also like