Abcd of SQL
Abcd of SQL
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;