0% found this document useful (0 votes)
24 views4 pages

Abcd of SQL

The document contains multiple SQL queries related to order statistics, employee data management, and revenue calculations. It includes queries to retrieve top categories by order count, revenue by category, and order counts by country, as well as creating and inserting data into an EMPLOYEE table. Additionally, it features salary calculations and ranking based on department salaries.

Uploaded by

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

Abcd of SQL

The document contains multiple SQL queries related to order statistics, employee data management, and revenue calculations. It includes queries to retrieve top categories by order count, revenue by category, and order counts by country, as well as creating and inserting data into an EMPLOYEE table. Additionally, it features salary calculations and ranking based on department salaries.

Uploaded by

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

Q1)

SELECT top 3 CategoryName, NumberOfOrders FROM


(
SELECT Categories.CategoryName, COUNT(OrderDetails.ProductID) AS NumberOfOrders
FROM ((Products
INNER JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID)
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID)
INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID
WHERE (Orders.OrderDate) >= #1996-07-01# AND (Orders.OrderDate) < #1996-09-01#
GROUP BY Categories.CategoryName
) AS Subquery

ORDER BY NumberOfOrders DESC;

Q2)

SELECT TOP 1
Categories.CategoryName,
SUM(OrderDetails.Quantity * Products.Price) AS Revenue
FROM
((Products
INNER JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID)
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID)
INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID
WHERE
Orders.OrderDate >= #1996-01-01# AND Orders.OrderDate < #1997-01-01#
GROUP BY
Categories.CategoryName
ORDER BY
SUM(OrderDetails.Quantity * Products.Price) DESC;

Q3)
SELECT TOP 1
Customers.Country AS CountryName,
COUNT(OrderDetails.ProductID) AS NumberOfOrders
FROM
((Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID)
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID)
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE
Products.CategoryID = (
SELECT CategoryID
FROM Categories
WHERE CategoryName = 'Seafood'
)
AND YEAR(Orders.OrderDate) = 1998
GROUP BY
Customers.Country
ORDER BY
COUNT(OrderDetails.ProductID) DESC;

Q5)

SELECT TOP 1
Shippers.ShipperName,
COUNT(Orders.ShipperID) AS TotalNumberOfOrdersShipped
FROM
Orders
INNER JOIN
Shippers ON Orders.ShipperID = Shippers.ShipperID
WHERE
YEAR(Orders.OrderDate) = 1996
GROUP BY
Shippers.ShipperName
ORDER BY
COUNT(Orders.ShipperID) DESC;

Q6)
-- create
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
fname TEXT NOT NULL,
lname text NOT NULL,
salary INTEGER,
dept TEXT NOT NULL
);

-- insert
INSERT INTO EMPLOYEE VALUES (0001, 'Clark','Biden',1000,'Sales');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave', 'Trump',2000,'Accounting');
INSERT INTO EMPLOYEE VALUES (0003, 'Ava', 'Sauerland',3000,'Sales');
INSERT INTO EMPLOYEE VALUES (0004, 'Arpit', '',4000,'Sales');
INSERT INTO EMPLOYEE VALUES (0005, 'Rohit', '',5000,'Sales');
INSERT INTO EMPLOYEE VALUES (0006, 'Jai', '',6000,'HR');
INSERT INTO EMPLOYEE VALUES (0007, 'Pallavi', '',7000,'HR');
INSERT INTO EMPLOYEE VALUES (0008, 'Balaji', '',8000,'HR');

SELECT
CASE
WHEN lname IS NULL THEN fname
ELSE CONCAT(fname, ' ', lname)

END AS Full_Name,
salary,
CASE
WHEN salary > 3000 THEN 'Y'
ELSE 'N'
END AS High_salary_flag
FROM
EMPLOYEE;

Q7)

-- create
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
fname TEXT NOT NULL,
lname text NOT NULL,
salary INTEGER,
dept TEXT NOT NULL
);

-- insert
INSERT INTO EMPLOYEE VALUES (0001, 'Clark','Biden',1000,'Sales');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave', 'Trump',2000,'Accounting');
INSERT INTO EMPLOYEE VALUES (0003, 'Ava', 'Sauerland',3000,'Sales');
INSERT INTO EMPLOYEE VALUES (0004, 'Arpit', '',4000,'Sales');
INSERT INTO EMPLOYEE VALUES (0005, 'Rohit', '',5000,'Sales');
INSERT INTO EMPLOYEE VALUES (0006, 'Jai', '',6000,'HR');
INSERT INTO EMPLOYEE VALUES (0007, 'Pallavi', '',7000,'HR');
INSERT INTO EMPLOYEE VALUES (0008, 'Balaji', '',8000,'HR');

SELECT
dept,
SUM(salary) AS total_salary
FROM
EMPLOYEE
GROUP BY
dept;

Q8)

-- create
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
fname TEXT NOT NULL,
lname text NOT NULL,
salary INTEGER,
dept TEXT NOT NULL
);
-- insert
INSERT INTO EMPLOYEE VALUES (0001, 'Clark','Biden',1000,'Sales');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave', 'Trump',2000,'Accounting');
INSERT INTO EMPLOYEE VALUES (0003, 'Ava', 'Sauerland',3000,'Sales');
INSERT INTO EMPLOYEE VALUES (0004, 'Arpit', '',4000,'Sales');
INSERT INTO EMPLOYEE VALUES (0005, 'Rohit', '',5000,'Sales');
INSERT INTO EMPLOYEE VALUES (0006, 'Jai', '',6000,'HR');
INSERT INTO EMPLOYEE VALUES (0007, 'Pallavi', '',7000,'HR');
INSERT INTO EMPLOYEE VALUES (0008, 'Balaji', '',8000,'HR');

SELECT
dept AS Department_name,
SUM(salary) AS Total_Salary,
RANK() OVER (ORDER BY SUM(salary) DESC) AS Salary_rank
FROM
EMPLOYEE
GROUP BY
dept;

You might also like