0% found this document useful (0 votes)
2 views

Queries.docx

The document provides SQL queries for various database operations including counting rows and columns, finding NULL values, aggregating sales data by ship mode, creating views, and identifying top cities by customer count. It also includes operations for comparing sales between regions, using correlated sub-queries, performing self joins, and deleting duplicate records in a table. Additionally, it demonstrates how to create and manipulate tables for products and employees.

Uploaded by

gargojas31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Queries.docx

The document provides SQL queries for various database operations including counting rows and columns, finding NULL values, aggregating sales data by ship mode, creating views, and identifying top cities by customer count. It also includes operations for comparing sales between regions, using correlated sub-queries, performing self joins, and deleting duplicate records in a table. Additionally, it demonstrates how to create and manipulate tables for products and employees.

Uploaded by

gargojas31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1) How many rows a table has?

Select COUNT(*) as C from orders

2) How many columns a table has?

SELECT count(*) AS NOC FROM information_schema.columns


WHERE table_name = 'Orders';

3) How many Order IDs are NULL?

SELECT SUM(CASE WHEN [Order ID] is null THEN 1 ELSE 0 END)


AS [Number Of Null Values] from Orders

4) Find out the Max, Min, Count, Sum, Average Sales for each Ship Mode

Select [Ship Mode], sum(sales) as TS


from Orders group by [Ship Mode]

5) Create a Shortcut (View) for Query1

Create view vw_SMTS


AS

6) Which are the Top 10 cities with maximum number of Customers?

Select top 10 * from


(select City, count([Customer ID]) as TT from Orders group by City) as TT order by TT
desc

7) How many Customers have placed more than 30 orders?

Select [Customer ID], count([Order ID]) as CO from Orders group by [Customer ID]
having count([Order ID])>30 order by CO desc

8) How good West region is as compared to East region with respect to total sales (Multiple)?

Select
(Select sum(sales) as TS from orders where Region='West')
/
(Select sum(sales) as TS from orders where Region='East') as W_Vs_E

9) Correlated Sub-Query
create table Details(
Product_id varchar(255),
Product_name varchar(255),
category varchar(255),
List_price int)

Insert into details


Values('P1', 'Pencil','1', 200), ('P2', 'Pen','2', 300), ('P3', 'Erazer','3', 250),
('P4', 'Sharpener','1', 400),
('P5', 'Notebook','2', 380), ('P6', 'Pen','3', 350)

select * from Details

-- Finds the products whose list price is equal to the highest list price of the
products within the same category

SELECT
product_name,
list_price,
category
FROM
Details d1
WHERE
list_price IN (
SELECT
MAX (d2.list_price)
FROM
Details d2
WHERE
d2.category = d1.category
GROUP BY
d2.category
)
ORDER BY
Category, product_name;

10) Self Join

Create table Employees(


EmpID varchar(255),
FN varchar(255),
Salary int,
Managerid varchar(255))

Insert into Employees


Values(1, 'Bharti', 1000, 3), (2, 'Deepak', 1500, 3), (3, 'Adhyan', 1800, 4)

Insert into Employees (EmpID, FN, Salary)


Values(4, 'Surya', 1200)

Insert into Employees


Values(5, 'Devshree', 1400, 1)

select * from Employees


SELECT
employee.EmpID,
employee.FN,
employee.ManagerId,
manager.FN as ManagerName
FROM Employees employee
JOIN Employees manager
ON employee.ManagerId = manager.EmpID

11) Delete duplicates

alter table orders


add ID int identity(1,1)

DELETE FROM Orders


WHERE [ID] NOT IN
(
SELECT MAX([ID]) AS MaxRecordID
FROM Orders
GROUP BY [Order ID], [Order Date],
[Ship Date],
[Ship Mode],
[Customer ID],
[Customer Name],
segment, Country, City, State, [Postal Code], Region,
[Product ID], Category, [Sub-Category], [Product Name], Sales, Quantity, Discount,
Profit
);

You might also like