├── lesson-1 └── homework │ └── homework.md ├── lesson-3 └── homework │ └── homework.md ├── lesson-2 └── homework │ └── homework.md ├── lesson-6 └── homework │ └── practise.md ├── lesson-21 └── homework │ └── homework.md ├── lesson-15 └── homework │ └── homework.md ├── lesson-16 └── homework │ └── homework.md ├── lesson-22 └── homework │ └── homework.md ├── lesson-19 └── homework │ └── homework.md ├── lesson-20 └── homework │ └── homework.md ├── lesson-18 └── homework │ └── practise.md ├── lesson-23 └── homework │ └── homework.md ├── lesson-17 └── homework │ └── homework.md ├── lesson-12 └── homework │ └── homework.md ├── lesson-8 └── homework │ └── homework.md ├── lesson-7 └── homework │ └── homework.md ├── lesson-4 └── homework │ └── homework.md ├── lesson-5 └── homework │ └── homework.md └── lesson-9 └── homework └── homework.md /lesson-1/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 1: Introduction to SQL Server and SSMS 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | ## Easy 11 | 1. Define the following terms: data, database, relational database, and table. 12 | 2. List five key features of SQL Server. 13 | 3. What are the different authentication modes available when connecting to SQL Server? (Give at least 2) 14 | 15 | ## Medium 16 | 4. Create a new database in SSMS named SchoolDB. 17 | 5. Write and execute a query to create a table called Students with columns: StudentID (INT, PRIMARY KEY), Name (VARCHAR(50)), Age (INT). 18 | 6. Describe the differences between SQL Server, SSMS, and SQL. 19 | 20 | ## Hard 21 | 7. Research and explain the different SQL commands: DQL, DML, DDL, DCL, TCL with examples. 22 | 8. Write a query to insert three records into the Students table. 23 | 9. Restore AdventureWorksDW2022.bak file to your server. (write its steps to submit) 24 | You can find the database from this link :`https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorksDW2022.bak` 25 | -------------------------------------------------------------------------------- /lesson-3/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 3: Importing and Exporting Data 2 | 3 | ✅ Importing Data (BULK INSERT, Excel, Text) 4 | ✅ Exporting Data (Excel, Text) 5 | ✅ Comments, Identity column, NULL/NOT NULL values 6 | ✅ Unique Key, Primary Key, Foreign Key, Check Constraint 7 | ✅ Differences between UNIQUE KEY and PRIMARY KEY 8 | 9 | > **Notes before doing the tasks:** 10 | > - Tasks should be solved using **SQL Server**. 11 | > - Case insensitivity applies. 12 | > - Alias names do not affect the score. 13 | > - Scoring is based on the **correct output**. 14 | > - One correct solution is sufficient. 15 | 16 | ______________________________________ 17 | 18 | ## 🟢 Easy-Level Tasks (10) 19 | 1. Define and explain the purpose of BULK INSERT in SQL Server. 20 | 2. List four file formats that can be imported into SQL Server. 21 | 3. Create a table Products with columns: ProductID (INT, PRIMARY KEY), ProductName (VARCHAR(50)), Price (DECIMAL(10,2)). 22 | 4. Insert three records into the Products table using INSERT INTO. 23 | 5. Explain the difference between NULL and NOT NULL. 24 | 6. Add a UNIQUE constraint to the ProductName column in the Products table. 25 | 7. Write a comment in a SQL query explaining its purpose. 26 | 8. Add CategoryID column to the Products table. 27 | 9. Create a table Categories with a CategoryID as PRIMARY KEY and a CategoryName as UNIQUE. 28 | 10. Explain the purpose of the IDENTITY column in SQL Server. 29 | 30 | ________________________________________ 31 | 32 | ## 🟠 Medium-Level Tasks (10) 33 | 11. Use BULK INSERT to import data from a text file into the Products table. 34 | 12. Create a FOREIGN KEY in the Products table that references the Categories table. 35 | 13. Explain the differences between PRIMARY KEY and UNIQUE KEY. 36 | 14. Add a CHECK constraint to the Products table ensuring Price > 0. 37 | 15. Modify the Products table to add a column Stock (INT, NOT NULL). 38 | 16. Use the ISNULL function to replace NULL values in Price column with a 0. 39 | 17. Describe the purpose and usage of FOREIGN KEY constraints in SQL Server. 40 | 41 | ________________________________________ 42 | 43 | ## 🔴 Hard-Level Tasks (10) 44 | 18. Write a script to create a Customers table with a CHECK constraint ensuring Age >= 18. 45 | 19. Create a table with an IDENTITY column starting at 100 and incrementing by 10. 46 | 20. Write a query to create a composite PRIMARY KEY in a new table OrderDetails. 47 | 21. Explain the use of COALESCE and ISNULL functions for handling NULL values. 48 | 22. Create a table Employees with both PRIMARY KEY on EmpID and UNIQUE KEY on Email. 49 | 23. Write a query to create a FOREIGN KEY with ON DELETE CASCADE and ON UPDATE CASCADE options. 50 | -------------------------------------------------------------------------------- /lesson-2/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 2: DDL and DML Commands 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | --- 11 | 12 | ### **Basic-Level Tasks (10)** 13 | 1. Create a table `Employees` with columns: `EmpID` INT, `Name` (VARCHAR(50)), and `Salary` (DECIMAL(10,2)). 14 | 2. Insert three records into the `Employees` table using different INSERT INTO approaches (single-row insert and multiple-row insert). 15 | 3. Update the `Salary` of an employee to `7000` where `EmpID = 1`. 16 | 4. Delete a record from the `Employees` table where `EmpID = 2`. 17 | 5. Give a brief definition for difference between `DELETE`, `TRUNCATE`, and `DROP`. 18 | 6. Modify the `Name` column in the `Employees` table to `VARCHAR(100)`. 19 | 7. Add a new column `Department` (`VARCHAR(50)`) to the `Employees` table. 20 | 8. Change the data type of the `Salary` column to `FLOAT`. 21 | 9. Create another table `Departments` with columns `DepartmentID` (INT, PRIMARY KEY) and `DepartmentName` (VARCHAR(50)). 22 | 10. Remove all records from the `Employees` table without deleting its structure. 23 | 24 | --- 25 | 26 | ### **Intermediate-Level Tasks (6)** 27 | 11. Insert five records into the `Departments` table using `INSERT INTO SELECT` method(you can write anything you want as data). 28 | 12. Update the `Department` of all employees where `Salary > 5000` to 'Management'. 29 | 13. Write a query that removes all employees but keeps the table structure intact. 30 | 14. Drop the `Department` column from the `Employees` table. 31 | 15. Rename the `Employees` table to `StaffMembers` using SQL commands. 32 | 16. Write a query to completely remove the `Departments` table from the database. 33 | 34 | --- 35 | 36 | ### **Advanced-Level Tasks (9)** 37 | 17. Create a table named Products with at least 5 columns, including: ProductID (Primary Key), ProductName (VARCHAR), Category (VARCHAR), Price (DECIMAL) 38 | 18. Add a CHECK constraint to ensure Price is always greater than 0. 39 | 19. Modify the table to add a StockQuantity column with a DEFAULT value of 50. 40 | 20. Rename Category to ProductCategory 41 | 21. Insert 5 records into the Products table using standard INSERT INTO queries. 42 | 22. Use SELECT INTO to create a backup table called Products_Backup containing all Products data. 43 | 23. Rename the Products table to Inventory. 44 | 24. Alter the Inventory table to change the data type of Price from DECIMAL(10,2) to FLOAT. 45 | 25. Add an IDENTITY column named ProductCode that starts from 1000 and increments by 5 to `Inventory` table. 46 | -------------------------------------------------------------------------------- /lesson-6/homework/practise.md: -------------------------------------------------------------------------------- 1 | # Lesson-6: Practice 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | ## Puzzle 1: Finding Distinct Values 11 | Question: Explain at least two ways to find distinct values based on two columns. 12 | 13 | **Input table (`InputTbl`):** 14 | ``` 15 | | col1 | col2 | 16 | |------|------| 17 | | a | b | 18 | | a | b | 19 | | b | a | 20 | | c | d | 21 | | c | d | 22 | | m | n | 23 | | n | m | 24 | ``` 25 | **Result should be like this:** 26 | ``` 27 | | col1 | col2 | 28 | |------|------| 29 | | a | b | 30 | | c | d | 31 | | m | n | 32 | ``` 33 | 34 | ```sql 35 | CREATE TABLE InputTbl ( 36 | col1 VARCHAR(10), 37 | col2 VARCHAR(10) 38 | ); 39 | INSERT INTO InputTbl (col1, col2) VALUES 40 | ('a', 'b'), 41 | ('a', 'b'), 42 | ('b', 'a'), 43 | ('c', 'd'), 44 | ('c', 'd'), 45 | ('m', 'n'), 46 | ('n', 'm'); 47 | ``` 48 | 49 | --- 50 | 51 | ## Puzzle 2: Removing Rows with All Zeroes 52 | **Question: If all the columns have zero values, then don’t show that row. In this case, we have to remove the 5th row while selecting data.** 53 | 54 | ### Table Schema: 55 | ```sql 56 | CREATE TABLE TestMultipleZero ( 57 | A INT NULL, 58 | B INT NULL, 59 | C INT NULL, 60 | D INT NULL 61 | ); 62 | 63 | INSERT INTO TestMultipleZero(A,B,C,D) 64 | VALUES 65 | (0,0,0,1), 66 | (0,0,1,0), 67 | (0,1,0,0), 68 | (1,0,0,0), 69 | (0,0,0,0), 70 | (1,1,1,0); 71 | ``` 72 | --- 73 | 74 | ## Puzzle 3: Find those with odd ids 75 | ```sql 76 | create table section1(id int, name varchar(20)) 77 | insert into section1 values (1, 'Been'), 78 | (2, 'Roma'), 79 | (3, 'Steven'), 80 | (4, 'Paulo'), 81 | (5, 'Genryh'), 82 | (6, 'Bruno'), 83 | (7, 'Fred'), 84 | (8, 'Andro') 85 | ``` 86 | ## Puzzle 4: Person with the smallest id (use the table in puzzle 3) 87 | 88 | --- 89 | 90 | ## Puzzle 5: Person with the highest id (use the table in puzzle 3) 91 | 92 | --- 93 | 94 | ## Puzzle 6: People whose name starts with b (use the table in puzzle 3) 95 | 96 | --- 97 | 98 | ## Puzle 7: Write a query to return only the rows where the code contains the literal underscore _ (not as a wildcard). 99 | ```sql 100 | CREATE TABLE ProductCodes ( 101 | Code VARCHAR(20) 102 | ); 103 | 104 | INSERT INTO ProductCodes (Code) VALUES 105 | ('X-123'), 106 | ('X_456'), 107 | ('X#789'), 108 | ('X-001'), 109 | ('X%202'), 110 | ('X_ABC'), 111 | ('X#DEF'), 112 | ('X-999'); 113 | ``` 114 | -------------------------------------------------------------------------------- /lesson-21/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 21 WINDOW FUNCTIONS 2 | 3 | 4 | > **Notes before doing the tasks:** 5 | > - Tasks should be solved using **SQL Server**. 6 | > - Case insensitivity applies. 7 | > - Alias names do not affect the score. 8 | > - Scoring is based on the **correct output**. 9 | > - One correct solution is sufficient. 10 | 11 | ```sql 12 | CREATE TABLE ProductSales ( 13 | SaleID INT PRIMARY KEY, 14 | ProductName VARCHAR(50) NOT NULL, 15 | SaleDate DATE NOT NULL, 16 | SaleAmount DECIMAL(10, 2) NOT NULL, 17 | Quantity INT NOT NULL, 18 | CustomerID INT NOT NULL 19 | ); 20 | INSERT INTO ProductSales (SaleID, ProductName, SaleDate, SaleAmount, Quantity, CustomerID) 21 | VALUES 22 | (1, 'Product A', '2023-01-01', 148.00, 2, 101), 23 | (2, 'Product B', '2023-01-02', 202.00, 3, 102), 24 | (3, 'Product C', '2023-01-03', 248.00, 1, 103), 25 | (4, 'Product A', '2023-01-04', 149.50, 4, 101), 26 | (5, 'Product B', '2023-01-05', 203.00, 5, 104), 27 | (6, 'Product C', '2023-01-06', 252.00, 2, 105), 28 | (7, 'Product A', '2023-01-07', 151.00, 1, 101), 29 | (8, 'Product B', '2023-01-08', 205.00, 8, 102), 30 | (9, 'Product C', '2023-01-09', 253.00, 7, 106), 31 | (10, 'Product A', '2023-01-10', 152.00, 2, 107), 32 | (11, 'Product B', '2023-01-11', 207.00, 3, 108), 33 | (12, 'Product C', '2023-01-12', 249.00, 1, 109), 34 | (13, 'Product A', '2023-01-13', 153.00, 4, 110), 35 | (14, 'Product B', '2023-01-14', 208.50, 5, 111), 36 | (15, 'Product C', '2023-01-15', 251.00, 2, 112), 37 | (16, 'Product A', '2023-01-16', 154.00, 1, 113), 38 | (17, 'Product B', '2023-01-17', 210.00, 8, 114), 39 | (18, 'Product C', '2023-01-18', 254.00, 7, 115), 40 | (19, 'Product A', '2023-01-19', 155.00, 3, 116), 41 | (20, 'Product B', '2023-01-20', 211.00, 4, 117), 42 | (21, 'Product C', '2023-01-21', 256.00, 2, 118), 43 | (22, 'Product A', '2023-01-22', 157.00, 5, 119), 44 | (23, 'Product B', '2023-01-23', 213.00, 3, 120), 45 | (24, 'Product C', '2023-01-24', 255.00, 1, 121), 46 | (25, 'Product A', '2023-01-25', 158.00, 6, 122), 47 | (26, 'Product B', '2023-01-26', 215.00, 7, 123), 48 | (27, 'Product C', '2023-01-27', 257.00, 3, 124), 49 | (28, 'Product A', '2023-01-28', 159.50, 4, 125), 50 | (29, 'Product B', '2023-01-29', 218.00, 5, 126), 51 | (30, 'Product C', '2023-01-30', 258.00, 2, 127); 52 | ``` 53 | 54 | 1. Write a query to assign a row number to each sale based on the SaleDate. 55 | 2. Write a query to rank products based on the total quantity sold. give the same rank for the same amounts without skipping numbers. 56 | 3. Write a query to identify the top sale for each customer based on the SaleAmount. 57 | 4. Write a query to display each sale's amount along with the next sale amount in the order of SaleDate. 58 | 5. Write a query to display each sale's amount along with the previous sale amount in the order of SaleDate. 59 | 6. Write a query to identify sales amounts that are greater than the previous sale's amount 60 | 7. Write a query to calculate the difference in sale amount from the previous sale for every product 61 | 8. Write a query to compare the current sale amount with the next sale amount in terms of percentage change. 62 | 9. Write a query to calculate the ratio of the current sale amount to the previous sale amount within the same product. 63 | 10. Write a query to calculate the difference in sale amount from the very first sale of that product. 64 | 11. Write a query to find sales that have been increasing continuously for a product 65 | (i.e., each sale amount is greater than the previous sale amount for that product). 66 | 12. Write a query to calculate a "closing balance"(running total) for sales amounts which adds the current sale amount to a running total of previous sales. 67 | 13. Write a query to calculate the moving average of sales amounts over the last 3 sales. 68 | 14. Write a query to show the difference between each sale amount and the average sale amount. 69 | 70 | ```sql 71 | CREATE TABLE Employees1 ( 72 | EmployeeID INT PRIMARY KEY, 73 | Name VARCHAR(50), 74 | Department VARCHAR(50), 75 | Salary DECIMAL(10,2), 76 | HireDate DATE 77 | ); 78 | 79 | INSERT INTO Employees1 (EmployeeID, Name, Department, Salary, HireDate) VALUES 80 | (1, 'John Smith', 'IT', 60000.00, '2020-03-15'), 81 | (2, 'Emma Johnson', 'HR', 50000.00, '2019-07-22'), 82 | (3, 'Michael Brown', 'Finance', 75000.00, '2018-11-10'), 83 | (4, 'Olivia Davis', 'Marketing', 55000.00, '2021-01-05'), 84 | (5, 'William Wilson', 'IT', 62000.00, '2022-06-12'), 85 | (6, 'Sophia Martinez', 'Finance', 77000.00, '2017-09-30'), 86 | (7, 'James Anderson', 'HR', 52000.00, '2020-04-18'), 87 | (8, 'Isabella Thomas', 'Marketing', 58000.00, '2019-08-25'), 88 | (9, 'Benjamin Taylor', 'IT', 64000.00, '2021-11-17'), 89 | (10, 'Charlotte Lee', 'Finance', 80000.00, '2016-05-09'), 90 | (11, 'Ethan Harris', 'IT', 63000.00, '2023-02-14'), 91 | (12, 'Mia Clark', 'HR', 53000.00, '2022-09-05'), 92 | (13, 'Alexander Lewis', 'Finance', 78000.00, '2015-12-20'), 93 | (14, 'Amelia Walker', 'Marketing', 57000.00, '2020-07-28'), 94 | (15, 'Daniel Hall', 'IT', 61000.00, '2018-10-13'), 95 | (16, 'Harper Allen', 'Finance', 79000.00, '2017-03-22'), 96 | (17, 'Matthew Young', 'HR', 54000.00, '2021-06-30'), 97 | (18, 'Ava King', 'Marketing', 56000.00, '2019-04-16'), 98 | (19, 'Lucas Wright', 'IT', 65000.00, '2022-12-01'), 99 | (20, 'Evelyn Scott', 'Finance', 81000.00, '2016-08-07'); 100 | ``` 101 | 102 | 15. Find Employees Who Have the Same Salary Rank 103 | 16. Identify the Top 2 Highest Salaries in Each Department 104 | 17. Find the Lowest-Paid Employee in Each Department 105 | 18. Calculate the Running Total of Salaries in Each Department 106 | 19. Find the Total Salary of Each Department Without GROUP BY 107 | 20. Calculate the Average Salary in Each Department Without GROUP BY 108 | 21. Find the Difference Between an Employee’s Salary and Their Department’s Average 109 | 22. Calculate the Moving Average Salary Over 3 Employees (Including Current, Previous, and Next) 110 | 23. Find the Sum of Salaries for the Last 3 Hired Employees 111 | -------------------------------------------------------------------------------- /lesson-15/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson-15: Subqueries and Exists 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | 11 | 12 | ## Level 1: Basic Subqueries 13 | 14 | # 1. Find Employees with Minimum Salary 15 | 16 | **Task: Retrieve employees who earn the minimum salary in the company.** 17 | **Tables: employees (columns: id, name, salary)** 18 | 19 | ```sql 20 | CREATE TABLE employees ( 21 | id INT PRIMARY KEY, 22 | name VARCHAR(100), 23 | salary DECIMAL(10, 2) 24 | ); 25 | 26 | INSERT INTO employees (id, name, salary) VALUES 27 | (1, 'Alice', 50000), 28 | (2, 'Bob', 60000), 29 | (3, 'Charlie', 50000); 30 | ``` 31 | 32 | # 2. Find Products Above Average Price 33 | 34 | **Task: Retrieve products priced above the average price.** 35 | **Tables: products (columns: id, product_name, price)** 36 | ```sql 37 | CREATE TABLE products ( 38 | id INT PRIMARY KEY, 39 | product_name VARCHAR(100), 40 | price DECIMAL(10, 2) 41 | ); 42 | 43 | INSERT INTO products (id, product_name, price) VALUES 44 | (1, 'Laptop', 1200), 45 | (2, 'Tablet', 400), 46 | (3, 'Smartphone', 800), 47 | (4, 'Monitor', 300); 48 | ``` 49 | --- 50 | 51 | ## Level 2: Nested Subqueries with Conditions 52 | 53 | **3. Find Employees in Sales Department** 54 | **Task: Retrieve employees who work in the "Sales" department.** 55 | **Tables: employees (columns: id, name, department_id), departments (columns: id, department_name)** 56 | ```sql 57 | CREATE TABLE departments ( 58 | id INT PRIMARY KEY, 59 | department_name VARCHAR(100) 60 | ); 61 | 62 | CREATE TABLE employees ( 63 | id INT PRIMARY KEY, 64 | name VARCHAR(100), 65 | department_id INT, 66 | FOREIGN KEY (department_id) REFERENCES departments(id) 67 | ); 68 | 69 | INSERT INTO departments (id, department_name) VALUES 70 | (1, 'Sales'), 71 | (2, 'HR'); 72 | 73 | INSERT INTO employees (id, name, department_id) VALUES 74 | (1, 'David', 1), 75 | (2, 'Eve', 2), 76 | (3, 'Frank', 1); 77 | ``` 78 | 79 | # 4. Find Customers with No Orders 80 | 81 | **Task: Retrieve customers who have not placed any orders.** 82 | **Tables: customers (columns: customer_id, name), orders (columns: order_id, customer_id)** 83 | ```sql 84 | CREATE TABLE customers ( 85 | customer_id INT PRIMARY KEY, 86 | name VARCHAR(100) 87 | ); 88 | 89 | CREATE TABLE orders ( 90 | order_id INT PRIMARY KEY, 91 | customer_id INT, 92 | FOREIGN KEY (customer_id) REFERENCES customers(customer_id) 93 | ); 94 | 95 | INSERT INTO customers (customer_id, name) VALUES 96 | (1, 'Grace'), 97 | (2, 'Heidi'), 98 | (3, 'Ivan'); 99 | 100 | INSERT INTO orders (order_id, customer_id) VALUES 101 | (1, 1), 102 | (2, 1); 103 | ``` 104 | --- 105 | 106 | ## Level 3: Aggregation and Grouping in Subqueries 107 | 108 | # 5. Find Products with Max Price in Each Category 109 | 110 | **Task: Retrieve products with the highest price in each category.** 111 | **Tables: products (columns: id, product_name, price, category_id)** 112 | ```sql 113 | CREATE TABLE products ( 114 | id INT PRIMARY KEY, 115 | product_name VARCHAR(100), 116 | price DECIMAL(10, 2), 117 | category_id INT 118 | ); 119 | 120 | INSERT INTO products (id, product_name, price, category_id) VALUES 121 | (1, 'Tablet', 400, 1), 122 | (2, 'Laptop', 1500, 1), 123 | (3, 'Headphones', 200, 2), 124 | (4, 'Speakers', 300, 2); 125 | ``` 126 | 127 | # 6. Find Employees in Department with Highest Average Salary 128 | 129 | **Task: Retrieve employees working in the department with the highest average salary.** 130 | **Tables: employees (columns: id, name, salary, department_id), departments (columns: id, department_name)** 131 | ```sql 132 | CREATE TABLE departments ( 133 | id INT PRIMARY KEY, 134 | department_name VARCHAR(100) 135 | ); 136 | 137 | CREATE TABLE employees ( 138 | id INT PRIMARY KEY, 139 | name VARCHAR(100), 140 | salary DECIMAL(10, 2), 141 | department_id INT, 142 | FOREIGN KEY (department_id) REFERENCES departments(id) 143 | ); 144 | 145 | INSERT INTO departments (id, department_name) VALUES 146 | (1, 'IT'), 147 | (2, 'Sales'); 148 | 149 | INSERT INTO employees (id, name, salary, department_id) VALUES 150 | (1, 'Jack', 80000, 1), 151 | (2, 'Karen', 70000, 1), 152 | (3, 'Leo', 60000, 2); 153 | ``` 154 | --- 155 | 156 | ## Level 4: Correlated Subqueries 157 | 158 | # 7. Find Employees Earning Above Department Average 159 | 160 | **Task: Retrieve employees earning more than the average salary in their department.** 161 | **Tables: employees (columns: id, name, salary, department_id)** 162 | ```sql 163 | CREATE TABLE employees ( 164 | id INT PRIMARY KEY, 165 | name VARCHAR(100), 166 | salary DECIMAL(10, 2), 167 | department_id INT 168 | ); 169 | 170 | INSERT INTO employees (id, name, salary, department_id) VALUES 171 | (1, 'Mike', 50000, 1), 172 | (2, 'Nina', 75000, 1), 173 | (3, 'Olivia', 40000, 2), 174 | (4, 'Paul', 55000, 2); 175 | ``` 176 | 177 | # 8. Find Students with Highest Grade per Course 178 | 179 | **Task: Retrieve students who received the highest grade in each course.** 180 | **Tables: students (columns: student_id, name), grades (columns: student_id, course_id, grade)** 181 | ```sql 182 | CREATE TABLE students ( 183 | student_id INT PRIMARY KEY, 184 | name VARCHAR(100) 185 | ); 186 | 187 | CREATE TABLE grades ( 188 | student_id INT, 189 | course_id INT, 190 | grade DECIMAL(4, 2), 191 | FOREIGN KEY (student_id) REFERENCES students(student_id) 192 | ); 193 | 194 | INSERT INTO students (student_id, name) VALUES 195 | (1, 'Sarah'), 196 | (2, 'Tom'), 197 | (3, 'Uma'); 198 | 199 | INSERT INTO grades (student_id, course_id, grade) VALUES 200 | (1, 101, 95), 201 | (2, 101, 85), 202 | (3, 102, 90), 203 | (1, 102, 80); 204 | ``` 205 | --- 206 | 207 | ## Level 5: Subqueries with Ranking and Complex Conditions 208 | 209 | **9. Find Third-Highest Price per Category** 210 | **Task: Retrieve products with the third-highest price in each category.** 211 | **Tables: products (columns: id, product_name, price, category_id)** 212 | ```sql 213 | CREATE TABLE products ( 214 | id INT PRIMARY KEY, 215 | product_name VARCHAR(100), 216 | price DECIMAL(10, 2), 217 | category_id INT 218 | ); 219 | 220 | INSERT INTO products (id, product_name, price, category_id) VALUES 221 | (1, 'Phone', 800, 1), 222 | (2, 'Laptop', 1500, 1), 223 | (3, 'Tablet', 600, 1), 224 | (4, 'Smartwatch', 300, 1), 225 | (5, 'Headphones', 200, 2), 226 | (6, 'Speakers', 300, 2), 227 | (7, 'Earbuds', 100, 2); 228 | ``` 229 | 230 | # 10. Find Employees whose Salary Between Company Average and Department Max Salary 231 | 232 | **Task: Retrieve employees with salaries above the company average but below the maximum in their department.** 233 | **Tables: employees (columns: id, name, salary, department_id)** 234 | ```sql 235 | CREATE TABLE employees ( 236 | id INT PRIMARY KEY, 237 | name VARCHAR(100), 238 | salary DECIMAL(10, 2), 239 | department_id INT 240 | ); 241 | 242 | INSERT INTO employees (id, name, salary, department_id) VALUES 243 | (1, 'Alex', 70000, 1), 244 | (2, 'Blake', 90000, 1), 245 | (3, 'Casey', 50000, 2), 246 | (4, 'Dana', 60000, 2), 247 | (5, 'Evan', 75000, 1); 248 | ``` 249 | 250 | 251 | -------------------------------------------------------------------------------- /lesson-16/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson-16: CTEs and Derived Tables 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | --- 11 | # Easy Tasks 12 | 13 | 1. Create a numbers table using a recursive query from 1 to 1000. 14 | 2. Write a query to find the total sales per employee using a derived table.(Sales, Employees) 15 | 3. Create a CTE to find the average salary of employees.(Employees) 16 | 4. Write a query using a derived table to find the highest sales for each product.(Sales, Products) 17 | 5. Beginning at 1, write a statement to double the number for each record, the max value you get should be less than 1000000. 18 | 6. Use a CTE to get the names of employees who have made more than 5 sales.(Sales, Employees) 19 | 7. Write a query using a CTE to find all products with sales greater than $500.(Sales, Products) 20 | 8. Create a CTE to find employees with salaries above the average salary.(Employees) 21 | 22 | 23 | # Medium Tasks 24 | 1. Write a query using a derived table to find the top 5 employees by the number of orders made.(Employees, Sales) 25 | 2. Write a query using a derived table to find the sales per product category.(Sales, Products) 26 | 3. Write a script to return the factorial of each value next to it.(Numbers1) 27 | 4. This script uses recursion to split a string into rows of substrings for each character in the string.(Example) 28 | 5. Use a CTE to calculate the sales difference between the current month and the previous month.(Sales) 29 | 6. Create a derived table to find employees with sales over $45000 in each quarter.(Sales, Employees) 30 | 31 | 32 | # Difficult Tasks 33 | 1. This script uses recursion to calculate Fibonacci numbers 34 | 2. Find a string where all characters are the same and the length is greater than 1.(FindSameCharacters) 35 | 3. Create a numbers table that shows all numbers 1 through n and their order gradually increasing by the next number in the sequence.(Example:n=5 | 1, 12, 123, 1234, 12345) 36 | 4. Write a query using a derived table to find the employees who have made the most sales in the last 6 months.(Employees,Sales) 37 | 5. Write a T-SQL query to remove the duplicate integer values present in the string column. Additionally, remove the single integer character that appears in the string.(RemoveDuplicateIntsFromNames) 38 | 39 | ```sql 40 | 41 | CREATE TABLE Numbers1(Number INT) 42 | 43 | INSERT INTO Numbers1 VALUES (5),(9),(8),(6),(7) 44 | 45 | CREATE TABLE FindSameCharacters 46 | ( 47 | Id INT 48 | ,Vals VARCHAR(10) 49 | ) 50 | 51 | INSERT INTO FindSameCharacters VALUES 52 | (1,'aa'), 53 | (2,'cccc'), 54 | (3,'abc'), 55 | (4,'aabc'), 56 | (5,NULL), 57 | (6,'a'), 58 | (7,'zzz'), 59 | (8,'abc') 60 | 61 | 62 | 63 | CREATE TABLE RemoveDuplicateIntsFromNames 64 | ( 65 | PawanName INT 66 | , Pawan_slug_name VARCHAR(1000) 67 | ) 68 | 69 | 70 | INSERT INTO RemoveDuplicateIntsFromNames VALUES 71 | (1, 'PawanA-111' ), 72 | (2, 'PawanB-123' ), 73 | (3, 'PawanB-32' ), 74 | (4, 'PawanC-4444' ), 75 | (5, 'PawanD-3' ) 76 | 77 | 78 | 79 | 80 | 81 | CREATE TABLE Example 82 | ( 83 | Id INTEGER IDENTITY(1,1) PRIMARY KEY, 84 | String VARCHAR(30) NOT NULL 85 | ); 86 | 87 | 88 | INSERT INTO Example VALUES('123456789'),('abcdefghi'); 89 | 90 | 91 | CREATE TABLE Employees ( 92 | EmployeeID INT PRIMARY KEY, 93 | DepartmentID INT, 94 | FirstName VARCHAR(50), 95 | LastName VARCHAR(50), 96 | Salary DECIMAL(10, 2) 97 | ); 98 | 99 | INSERT INTO Employees (EmployeeID, DepartmentID, FirstName, LastName, Salary) VALUES 100 | (1, 1, 'John', 'Doe', 60000.00), 101 | (2, 1, 'Jane', 'Smith', 65000.00), 102 | (3, 2, 'James', 'Brown', 70000.00), 103 | (4, 3, 'Mary', 'Johnson', 75000.00), 104 | (5, 4, 'Linda', 'Williams', 80000.00), 105 | (6, 2, 'Michael', 'Jones', 85000.00), 106 | (7, 1, 'Robert', 'Miller', 55000.00), 107 | (8, 3, 'Patricia', 'Davis', 72000.00), 108 | (9, 4, 'Jennifer', 'García', 77000.00), 109 | (10, 1, 'William', 'Martínez', 69000.00); 110 | 111 | CREATE TABLE Departments ( 112 | DepartmentID INT PRIMARY KEY, 113 | DepartmentName VARCHAR(50) 114 | ); 115 | 116 | INSERT INTO Departments (DepartmentID, DepartmentName) VALUES 117 | (1, 'HR'), 118 | (2, 'Sales'), 119 | (3, 'Marketing'), 120 | (4, 'Finance'), 121 | (5, 'IT'), 122 | (6, 'Operations'), 123 | (7, 'Customer Service'), 124 | (8, 'R&D'), 125 | (9, 'Legal'), 126 | (10, 'Logistics'); 127 | 128 | CREATE TABLE Sales ( 129 | SalesID INT PRIMARY KEY, 130 | EmployeeID INT, 131 | ProductID INT, 132 | SalesAmount DECIMAL(10, 2), 133 | SaleDate DATE 134 | ); 135 | INSERT INTO Sales (SalesID, EmployeeID, ProductID, SalesAmount, SaleDate) VALUES 136 | -- January 2025 137 | (1, 1, 1, 1550.00, '2025-01-02'), 138 | (2, 2, 2, 2050.00, '2025-01-04'), 139 | (3, 3, 3, 1250.00, '2025-01-06'), 140 | (4, 4, 4, 1850.00, '2025-01-08'), 141 | (5, 5, 5, 2250.00, '2025-01-10'), 142 | (6, 6, 6, 1450.00, '2025-01-12'), 143 | (7, 7, 1, 2550.00, '2025-01-14'), 144 | (8, 8, 2, 1750.00, '2025-01-16'), 145 | (9, 9, 3, 1650.00, '2025-01-18'), 146 | (10, 10, 4, 1950.00, '2025-01-20'), 147 | (11, 1, 5, 2150.00, '2025-02-01'), 148 | (12, 2, 6, 1350.00, '2025-02-03'), 149 | (13, 3, 1, 2050.00, '2025-02-05'), 150 | (14, 4, 2, 1850.00, '2025-02-07'), 151 | (15, 5, 3, 1550.00, '2025-02-09'), 152 | (16, 6, 4, 2250.00, '2025-02-11'), 153 | (17, 7, 5, 1750.00, '2025-02-13'), 154 | (18, 8, 6, 1650.00, '2025-02-15'), 155 | (19, 9, 1, 2550.00, '2025-02-17'), 156 | (20, 10, 2, 1850.00, '2025-02-19'), 157 | (21, 1, 3, 1450.00, '2025-03-02'), 158 | (22, 2, 4, 1950.00, '2025-03-05'), 159 | (23, 3, 5, 2150.00, '2025-03-08'), 160 | (24, 4, 6, 1700.00, '2025-03-11'), 161 | (25, 5, 1, 1600.00, '2025-03-14'), 162 | (26, 6, 2, 2050.00, '2025-03-17'), 163 | (27, 7, 3, 2250.00, '2025-03-20'), 164 | (28, 8, 4, 1350.00, '2025-03-23'), 165 | (29, 9, 5, 2550.00, '2025-03-26'), 166 | (30, 10, 6, 1850.00, '2025-03-29'), 167 | (31, 1, 1, 2150.00, '2025-04-02'), 168 | (32, 2, 2, 1750.00, '2025-04-05'), 169 | (33, 3, 3, 1650.00, '2025-04-08'), 170 | (34, 4, 4, 1950.00, '2025-04-11'), 171 | (35, 5, 5, 2050.00, '2025-04-14'), 172 | (36, 6, 6, 2250.00, '2025-04-17'), 173 | (37, 7, 1, 2350.00, '2025-04-20'), 174 | (38, 8, 2, 1800.00, '2025-04-23'), 175 | (39, 9, 3, 1700.00, '2025-04-26'), 176 | (40, 10, 4, 2000.00, '2025-04-29'), 177 | (41, 1, 5, 2200.00, '2025-05-03'), 178 | (42, 2, 6, 1650.00, '2025-05-07'), 179 | (43, 3, 1, 2250.00, '2025-05-11'), 180 | (44, 4, 2, 1800.00, '2025-05-15'), 181 | (45, 5, 3, 1900.00, '2025-05-19'), 182 | (46, 6, 4, 2000.00, '2025-05-23'), 183 | (47, 7, 5, 2400.00, '2025-05-27'), 184 | (48, 8, 6, 2450.00, '2025-05-31'), 185 | (49, 9, 1, 2600.00, '2025-06-04'), 186 | (50, 10, 2, 2050.00, '2025-06-08'), 187 | (51, 1, 3, 1550.00, '2025-06-12'), 188 | (52, 2, 4, 1850.00, '2025-06-16'), 189 | (53, 3, 5, 1950.00, '2025-06-20'), 190 | (54, 4, 6, 1900.00, '2025-06-24'), 191 | (55, 5, 1, 2000.00, '2025-07-01'), 192 | (56, 6, 2, 2100.00, '2025-07-05'), 193 | (57, 7, 3, 2200.00, '2025-07-09'), 194 | (58, 8, 4, 2300.00, '2025-07-13'), 195 | (59, 9, 5, 2350.00, '2025-07-17'), 196 | (60, 10, 6, 2450.00, '2025-08-01'); 197 | 198 | CREATE TABLE Products ( 199 | ProductID INT PRIMARY KEY, 200 | CategoryID INT, 201 | ProductName VARCHAR(100), 202 | Price DECIMAL(10, 2) 203 | ); 204 | 205 | INSERT INTO Products (ProductID, CategoryID, ProductName, Price) VALUES 206 | (1, 1, 'Laptop', 1000.00), 207 | (2, 1, 'Smartphone', 800.00), 208 | (3, 2, 'Tablet', 500.00), 209 | (4, 2, 'Monitor', 300.00), 210 | (5, 3, 'Headphones', 150.00), 211 | (6, 3, 'Mouse', 25.00), 212 | (7, 4, 'Keyboard', 50.00), 213 | (8, 4, 'Speaker', 200.00), 214 | (9, 5, 'Smartwatch', 250.00), 215 | (10, 5, 'Camera', 700.00); 216 | ``` 217 | 218 | 219 | 220 | -------------------------------------------------------------------------------- /lesson-22/homework/homework.md: -------------------------------------------------------------------------------- 1 | 2 | # Lesson 22: Aggregated Window Functions 3 | 4 | 5 | ```sql 6 | CREATE TABLE sales_data ( 7 | sale_id INT PRIMARY KEY, 8 | customer_id INT, 9 | customer_name VARCHAR(100), 10 | product_category VARCHAR(50), 11 | product_name VARCHAR(100), 12 | quantity_sold INT, 13 | unit_price DECIMAL(10,2), 14 | total_amount DECIMAL(10,2), 15 | order_date DATE, 16 | region VARCHAR(50) 17 | ); 18 | 19 | INSERT INTO sales_data VALUES 20 | (1, 101, 'Alice', 'Electronics', 'Laptop', 1, 1200.00, 1200.00, '2024-01-01', 'North'), 21 | (2, 102, 'Bob', 'Electronics', 'Phone', 2, 600.00, 1200.00, '2024-01-02', 'South'), 22 | (3, 103, 'Charlie', 'Clothing', 'T-Shirt', 5, 20.00, 100.00, '2024-01-03', 'East'), 23 | (4, 104, 'David', 'Furniture', 'Table', 1, 250.00, 250.00, '2024-01-04', 'West'), 24 | (5, 105, 'Eve', 'Electronics', 'Tablet', 1, 300.00, 300.00, '2024-01-05', 'North'), 25 | (6, 106, 'Frank', 'Clothing', 'Jacket', 2, 80.00, 160.00, '2024-01-06', 'South'), 26 | (7, 107, 'Grace', 'Electronics', 'Headphones', 3, 50.00, 150.00, '2024-01-07', 'East'), 27 | (8, 108, 'Hank', 'Furniture', 'Chair', 4, 75.00, 300.00, '2024-01-08', 'West'), 28 | (9, 109, 'Ivy', 'Clothing', 'Jeans', 1, 40.00, 40.00, '2024-01-09', 'North'), 29 | (10, 110, 'Jack', 'Electronics', 'Laptop', 2, 1200.00, 2400.00, '2024-01-10', 'South'), 30 | (11, 101, 'Alice', 'Electronics', 'Phone', 1, 600.00, 600.00, '2024-01-11', 'North'), 31 | (12, 102, 'Bob', 'Furniture', 'Sofa', 1, 500.00, 500.00, '2024-01-12', 'South'), 32 | (13, 103, 'Charlie', 'Electronics', 'Camera', 1, 400.00, 400.00, '2024-01-13', 'East'), 33 | (14, 104, 'David', 'Clothing', 'Sweater', 2, 60.00, 120.00, '2024-01-14', 'West'), 34 | (15, 105, 'Eve', 'Furniture', 'Bed', 1, 800.00, 800.00, '2024-01-15', 'North'), 35 | (16, 106, 'Frank', 'Electronics', 'Monitor', 1, 200.00, 200.00, '2024-01-16', 'South'), 36 | (17, 107, 'Grace', 'Clothing', 'Scarf', 3, 25.00, 75.00, '2024-01-17', 'East'), 37 | (18, 108, 'Hank', 'Furniture', 'Desk', 1, 350.00, 350.00, '2024-01-18', 'West'), 38 | (19, 109, 'Ivy', 'Electronics', 'Speaker', 2, 100.00, 200.00, '2024-01-19', 'North'), 39 | (20, 110, 'Jack', 'Clothing', 'Shoes', 1, 90.00, 90.00, '2024-01-20', 'South'), 40 | (21, 111, 'Kevin', 'Electronics', 'Mouse', 3, 25.00, 75.00, '2024-01-21', 'East'), 41 | (22, 112, 'Laura', 'Furniture', 'Couch', 1, 700.00, 700.00, '2024-01-22', 'West'), 42 | (23, 113, 'Mike', 'Clothing', 'Hat', 4, 15.00, 60.00, '2024-01-23', 'North'), 43 | (24, 114, 'Nancy', 'Electronics', 'Smartwatch', 1, 250.00, 250.00, '2024-01-24', 'South'), 44 | (25, 115, 'Oscar', 'Furniture', 'Wardrobe', 1, 1000.00, 1000.00, '2024-01-25', 'East') 45 | 46 | ``` 47 | 48 | 49 | 50 | ## Easy Questions 51 | 52 | 1. **Compute Running Total Sales per Customer** 53 | 2. **Count the Number of Orders per Product Category** 54 | 3. **Find the Maximum Total Amount per Product Category** 55 | 4. **Find the Minimum Price of Products per Product Category** 56 | 5. **Compute the Moving Average of Sales of 3 days (prev day, curr day, next day)** 57 | 6. **Find the Total Sales per Region** 58 | 7. **Compute the Rank of Customers Based on Their Total Purchase Amount** 59 | 8. **Calculate the Difference Between Current and Previous Sale Amount per Customer** 60 | 9. **Find the Top 3 Most Expensive Products in Each Category** 61 | 10. **Compute the Cumulative Sum of Sales Per Region by Order Date** 62 | 63 | --- 64 | 65 | ## Medium Questions 66 | 67 | 11. **Compute Cumulative Revenue per Product Category** 68 | 69 | 12. **Here you need to find out the sum of previous values. Please go through the sample input and expected output.** 70 | 71 | **Sample Input** 72 | ``` 73 | | ID | 74 | |----| 75 | | 1 | 76 | | 2 | 77 | | 3 | 78 | | 4 | 79 | | 5 | 80 | ``` 81 | 82 | **Expected Output** 83 | ``` 84 | | ID | SumPreValues | 85 | |----|--------------| 86 | | 1 | 1 | 87 | | 2 | 3 | 88 | | 3 | 6 | 89 | | 4 | 10 | 90 | | 5 | 15 | 91 | ``` 92 | --- 93 | 94 | 13. **Sum of Previous Values to Current Value** 95 | 96 | ```sql 97 | CREATE TABLE OneColumn ( 98 | Value SMALLINT 99 | ); 100 | INSERT INTO OneColumn VALUES (10), (20), (30), (40), (100); 101 | ``` 102 | 103 | **Sample Input** 104 | ``` 105 | | Value | 106 | |-------| 107 | | 10 | 108 | | 20 | 109 | | 30 | 110 | | 40 | 111 | | 100 | 112 | ``` 113 | **Expected Output** 114 | ``` 115 | | Value | Sum of Previous | 116 | |-------|-----------------| 117 | | 10 | 10 | 118 | | 20 | 30 | 119 | | 30 | 50 | 120 | | 40 | 70 | 121 | | 100 | 140 | 122 | ``` 123 | --- 124 | 125 | 14. **Find customers who have purchased items from more than one product_category** 126 | 15. **Find Customers with Above-Average Spending in Their Region** 127 | 16. **Rank customers based on their total spending (total_amount) within each region. If multiple customers have the same spending, they should receive the same rank.** 128 | 17. **Calculate the running total (cumulative_sales) of total_amount for each customer_id, ordered by order_date.** 129 | 18. **Calculate the sales growth rate (growth_rate) for each month compared to the previous month.** 130 | 19. **Identify customers whose total_amount is higher than their last order''s total_amount.(Table sales_data)** 131 | 132 | --- 133 | 134 | ## Hard Questions 135 | 136 | 20. **Identify Products that prices are above the average product price** 137 | 138 | 21. **In this puzzle you have to find the sum of val1 and val2 for each group and put that value at the beginning of the group in the new column. The challenge here is to do this in a single select. For more details please see the sample input and expected output.** 139 | 140 | ```sql 141 | CREATE TABLE MyData ( 142 | Id INT, Grp INT, Val1 INT, Val2 INT 143 | ); 144 | INSERT INTO MyData VALUES 145 | (1,1,30,29), (2,1,19,0), (3,1,11,45), (4,2,0,0), (5,2,100,17); 146 | ``` 147 | 148 | **Sample Input** 149 | ``` 150 | | Id | Grp | Val1 | Val2 | 151 | |-----|-----|------|------| 152 | | 1 | 1 | 30 | 29 | 153 | | 2 | 1 | 19 | 0 | 154 | | 3 | 1 | 11 | 45 | 155 | | 4 | 2 | 0 | 0 | 156 | | 5 | 2 | 100 | 17 | 157 | ``` 158 | 159 | 160 | **Expected Output** 161 | ``` 162 | | Id | Grp | Val1 | Val2 | Tot | 163 | |----|-----|------|------|------| 164 | | 1 | 1 | 30 | 29 | 134 | 165 | | 2 | 1 | 19 | 0 | NULL | 166 | | 3 | 1 | 11 | 45 | NULL | 167 | | 4 | 2 | 0 | 0 | 117 | 168 | | 5 | 2 | 100 | 17 | NULL | 169 | ``` 170 | --- 171 | 172 | 22. **Here you have to sum up the value of the cost column based on the values of Id. For Quantity if values are different then we have to add those values.Please go through the sample input and expected output for details.** 173 | 174 | ```sql 175 | CREATE TABLE TheSumPuzzle ( 176 | ID INT, Cost INT, Quantity INT 177 | ); 178 | INSERT INTO TheSumPuzzle VALUES 179 | (1234,12,164), (1234,13,164), (1235,100,130), (1235,100,135), (1236,12,136); 180 | ``` 181 | 182 | **Sample Input** 183 | ``` 184 | | Id | Cost | Quantity | 185 | |------|------|----------| 186 | | 1234 | 12 | 164 | 187 | | 1234 | 13 | 164 | 188 | | 1235 | 100 | 130 | 189 | | 1235 | 100 | 135 | 190 | | 1236 | 12 | 136 | 191 | ``` 192 | 193 | **Expected Output** 194 | ``` 195 | | Id | Cost | Quantity | 196 | |------|------|----------| 197 | | 1234 | 25 | 164 | 198 | | 1235 | 200 | 265 | 199 | | 1236 | 12 | 136 | 200 | ``` 201 | --- 202 | 203 | 23. **From following set of integers, write an SQL statement to determine the expected outputs** 204 | 205 | ```sql 206 | CREATE TABLE Seats 207 | ( 208 | SeatNumber INTEGER 209 | ); 210 | 211 | INSERT INTO Seats VALUES 212 | (7),(13),(14),(15),(27),(28),(29),(30), 213 | (31),(32),(33),(34),(35),(52),(53),(54); 214 | ``` 215 | 216 | **Output:** 217 | ``` 218 | --------------------- 219 | |Gap Start |Gap End| 220 | --------------------- 221 | | 1 | 6 | 222 | | 8 | 12 | 223 | | 16 | 26 | 224 | | 36 | 51 | 225 | --------------------- 226 | ``` 227 | 228 | -------------------------------------------------------------------------------- /lesson-19/homework/homework.md: -------------------------------------------------------------------------------- 1 | Lesson-19: Stored procedures, Merge and Practice 2 | 3 | Notes before doing the tasks: 4 | 5 | Tasks should be solved using SQL Server. 6 | Case insensitivity applies. 7 | Alias names do not affect the score. 8 | Scoring is based on the correct output. 9 | One correct solution is sufficient. 10 | 11 | 12 | --- 13 | 14 | # ✅ TASKS on **Stored Procedures** and **MERGE** 15 | 16 | --- 17 | 18 | # 🔵 Part 1: Stored Procedure Tasks 19 | 20 | ## Tables to use: 21 | 22 | ```sql 23 | CREATE TABLE Employees ( 24 | EmployeeID INT PRIMARY KEY, 25 | FirstName NVARCHAR(50), 26 | LastName NVARCHAR(50), 27 | Department NVARCHAR(50), 28 | Salary DECIMAL(10,2) 29 | ); 30 | 31 | CREATE TABLE DepartmentBonus ( 32 | Department NVARCHAR(50) PRIMARY KEY, 33 | BonusPercentage DECIMAL(5,2) 34 | ); 35 | 36 | INSERT INTO Employees VALUES 37 | (1, 'John', 'Doe', 'Sales', 5000), 38 | (2, 'Jane', 'Smith', 'Sales', 5200), 39 | (3, 'Mike', 'Brown', 'IT', 6000), 40 | (4, 'Anna', 'Taylor', 'HR', 4500); 41 | 42 | INSERT INTO DepartmentBonus VALUES 43 | ('Sales', 10), 44 | ('IT', 15), 45 | ('HR', 8); 46 | ``` 47 | 48 | --- 49 | 50 | ## 📄 Task 1: 51 | 52 | Create a stored procedure that: 53 | 54 | - Creates a temp table `#EmployeeBonus` 55 | - Inserts EmployeeID, FullName (FirstName + LastName), Department, Salary, and BonusAmount into it 56 | - (BonusAmount = Salary * BonusPercentage / 100) 57 | - Then, selects all data from the temp table. 58 | 59 | --- 60 | 61 | ## 📄 Task 2: 62 | 63 | Create a stored procedure that: 64 | 65 | - Accepts a department name and an increase percentage as parameters 66 | - Update salary of all employees in the given department by the given percentage 67 | - Returns updated employees from that department. 68 | 69 | --- 70 | 71 | # 🔵 Part 2: MERGE Tasks 72 | 73 | ## Tables to use: 74 | 75 | ```sql 76 | CREATE TABLE Products_Current ( 77 | ProductID INT PRIMARY KEY, 78 | ProductName NVARCHAR(100), 79 | Price DECIMAL(10,2) 80 | ); 81 | 82 | CREATE TABLE Products_New ( 83 | ProductID INT PRIMARY KEY, 84 | ProductName NVARCHAR(100), 85 | Price DECIMAL(10,2) 86 | ); 87 | 88 | INSERT INTO Products_Current VALUES 89 | (1, 'Laptop', 1200), 90 | (2, 'Tablet', 600), 91 | (3, 'Smartphone', 800); 92 | 93 | INSERT INTO Products_New VALUES 94 | (2, 'Tablet Pro', 700), 95 | (3, 'Smartphone', 850), 96 | (4, 'Smartwatch', 300); 97 | ``` 98 | 99 | --- 100 | 101 | ## 📄 Task 3: 102 | 103 | Perform a MERGE operation that: 104 | 105 | - Updates `ProductName` and `Price` if `ProductID` matches 106 | - Inserts new products if `ProductID` does not exist 107 | - Deletes products from `Products_Current` if they are missing in `Products_New` 108 | - Return the final state of `Products_Current` after the MERGE. 109 | 110 | --- 111 | 112 | ## 📄 Task 4: 113 | 114 | **Tree Node** 115 | 116 | Each node in the tree can be one of three types: 117 | 118 | - **"Leaf"**: if the node is a leaf node. 119 | - **"Root"**: if the node is the root of the tree. 120 | - **"Inner"**: If the node is neither a leaf node nor a root node. 121 | 122 | Write a solution to report the type of each node in the tree. 123 | 124 | Input: 125 | 126 | ```sql 127 | CREATE TABLE IF NOT EXISTS Tree (id INT, p_id INT); 128 | TRUNCATE TABLE Tree; 129 | INSERT INTO Tree (id, p_id) VALUES (1, NULL); 130 | INSERT INTO Tree (id, p_id) VALUES (2, 1); 131 | INSERT INTO Tree (id, p_id) VALUES (3, 1); 132 | INSERT INTO Tree (id, p_id) VALUES (4, 2); 133 | INSERT INTO Tree (id, p_id) VALUES (5, 2); 134 | ``` 135 | 136 | Output: 137 | 138 | | id | type | 139 | |-----|-------| 140 | | 1 | Root | 141 | | 2 | Inner | 142 | | 3 | Leaf | 143 | | 4 | Leaf | 144 | | 5 | Leaf | 145 | 146 | 🔗 [Solve this puzzle on LeetCode](https://leetcode.com/problems/tree-node/description/) 147 | 148 | --- 149 | 150 | ## 📄 Task 5: 151 | 152 | **Confirmation Rate** 153 | 154 | Find the confirmation rate for each user. If a user has no confirmation requests, the rate should be 0. 155 | 156 | Input: 157 | 158 | ```sql 159 | CREATE TABLE IF NOT EXISTS Signups (user_id INT, time_stamp DATETIME); 160 | CREATE TABLE IF NOT EXISTS Confirmations (user_id INT, time_stamp DATETIME, action ENUM('confirmed','timeout')); 161 | 162 | TRUNCATE TABLE Signups; 163 | INSERT INTO Signups (user_id, time_stamp) VALUES 164 | (3, '2020-03-21 10:16:13'), 165 | (7, '2020-01-04 13:57:59'), 166 | (2, '2020-07-29 23:09:44'), 167 | (6, '2020-12-09 10:39:37'); 168 | 169 | TRUNCATE TABLE Confirmations; 170 | INSERT INTO Confirmations (user_id, time_stamp, action) VALUES 171 | (3, '2021-01-06 03:30:46', 'timeout'), 172 | (3, '2021-07-14 14:00:00', 'timeout'), 173 | (7, '2021-06-12 11:57:29', 'confirmed'), 174 | (7, '2021-06-13 12:58:28', 'confirmed'), 175 | (7, '2021-06-14 13:59:27', 'confirmed'), 176 | (2, '2021-01-22 00:00:00', 'confirmed'), 177 | (2, '2021-02-28 23:59:59', 'timeout'); 178 | ``` 179 | 180 | Output: 181 | 182 | | user_id | confirmation_rate | 183 | |---------|-------------------| 184 | | 6 | 0.00 | 185 | | 3 | 0.00 | 186 | | 7 | 1.00 | 187 | | 2 | 0.50 | 188 | 189 | 🔗 [Solve this puzzle on LeetCode](https://leetcode.com/problems/confirmation-rate/description/) 190 | 191 | --- 192 | 193 | ## 📄 Task 6: 194 | 195 | **Find employees with the lowest salary** 196 | 197 | Input: 198 | 199 | ```sql 200 | CREATE TABLE employees ( 201 | id INT PRIMARY KEY, 202 | name VARCHAR(100), 203 | salary DECIMAL(10,2) 204 | ); 205 | 206 | INSERT INTO employees (id, name, salary) VALUES 207 | (1, 'Alice', 50000), 208 | (2, 'Bob', 60000), 209 | (3, 'Charlie', 50000); 210 | ``` 211 | 212 | - Find all employees who have the lowest salary using subqueries. 213 | 214 | --- 215 | 216 | ## 📄 Task 7: 217 | 218 | **Get Product Sales Summary** 219 | 220 | Input: 221 | 222 | ```sql 223 | -- Products Table 224 | CREATE TABLE Products ( 225 | ProductID INT PRIMARY KEY, 226 | ProductName NVARCHAR(100), 227 | Category NVARCHAR(50), 228 | Price DECIMAL(10,2) 229 | ); 230 | 231 | -- Sales Table 232 | CREATE TABLE Sales ( 233 | SaleID INT PRIMARY KEY, 234 | ProductID INT FOREIGN KEY REFERENCES Products(ProductID), 235 | Quantity INT, 236 | SaleDate DATE 237 | ); 238 | 239 | INSERT INTO Products (ProductID, ProductName, Category, Price) VALUES 240 | (1, 'Laptop Model A', 'Electronics', 1200), 241 | (2, 'Laptop Model B', 'Electronics', 1500), 242 | (3, 'Tablet Model X', 'Electronics', 600), 243 | (4, 'Tablet Model Y', 'Electronics', 700), 244 | (5, 'Smartphone Alpha', 'Electronics', 800), 245 | (6, 'Smartphone Beta', 'Electronics', 850), 246 | (7, 'Smartwatch Series 1', 'Wearables', 300), 247 | (8, 'Smartwatch Series 2', 'Wearables', 350), 248 | (9, 'Headphones Basic', 'Accessories', 150), 249 | (10, 'Headphones Pro', 'Accessories', 250), 250 | (11, 'Wireless Mouse', 'Accessories', 50), 251 | (12, 'Wireless Keyboard', 'Accessories', 80), 252 | (13, 'Desktop PC Standard', 'Computers', 1000), 253 | (14, 'Desktop PC Gaming', 'Computers', 2000), 254 | (15, 'Monitor 24 inch', 'Displays', 200), 255 | (16, 'Monitor 27 inch', 'Displays', 300), 256 | (17, 'Printer Basic', 'Office', 120), 257 | (18, 'Printer Pro', 'Office', 400), 258 | (19, 'Router Basic', 'Networking', 70), 259 | (20, 'Router Pro', 'Networking', 150); 260 | 261 | INSERT INTO Sales (SaleID, ProductID, Quantity, SaleDate) VALUES 262 | (1, 1, 2, '2024-01-15'), 263 | (2, 1, 1, '2024-02-10'), 264 | (3, 1, 3, '2024-03-08'), 265 | (4, 2, 1, '2024-01-22'), 266 | (5, 3, 5, '2024-01-20'), 267 | (6, 5, 2, '2024-02-18'), 268 | (7, 5, 1, '2024-03-25'), 269 | (8, 6, 4, '2024-04-02'), 270 | (9, 7, 2, '2024-01-30'), 271 | (10, 7, 1, '2024-02-25'), 272 | (11, 7, 1, '2024-03-15'), 273 | (12, 9, 8, '2024-01-18'), 274 | (13, 9, 5, '2024-02-20'), 275 | (14, 10, 3, '2024-03-22'), 276 | (15, 11, 2, '2024-02-14'), 277 | (16, 13, 1, '2024-03-10'), 278 | (17, 14, 2, '2024-03-22'), 279 | (18, 15, 5, '2024-02-01'), 280 | (19, 15, 3, '2024-03-11'), 281 | (20, 19, 4, '2024-04-01'); 282 | ``` 283 | 284 | Create a stored procedure called `GetProductSalesSummary` that: 285 | 286 | - Accepts a `@ProductID` input 287 | - Returns: 288 | - ProductName 289 | - Total Quantity Sold 290 | - Total Sales Amount (Quantity × Price) 291 | - First Sale Date 292 | - Last Sale Date 293 | - If the product has no sales, return `NULL` for quantity, total amount, first date, and last date, but still return the product name. 294 | 295 | --- 296 | 297 | 298 | 299 | 300 | -------------------------------------------------------------------------------- /lesson-20/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson-20: Practice 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | ```sql 11 | CREATE TABLE #Sales ( 12 | SaleID INT PRIMARY KEY IDENTITY(1,1), 13 | CustomerName VARCHAR(100), 14 | Product VARCHAR(100), 15 | Quantity INT, 16 | Price DECIMAL(10,2), 17 | SaleDate DATE 18 | ); 19 | 20 | 21 | INSERT INTO #Sales (CustomerName, Product, Quantity, Price, SaleDate) VALUES 22 | ('Alice', 'Laptop', 1, 1200.00, '2024-01-15'), 23 | ('Bob', 'Smartphone', 2, 800.00, '2024-02-10'), 24 | ('Charlie', 'Tablet', 1, 500.00, '2024-02-20'), 25 | ('David', 'Laptop', 1, 1300.00, '2024-03-05'), 26 | ('Eve', 'Smartphone', 3, 750.00, '2024-03-12'), 27 | ('Frank', 'Headphones', 2, 100.00, '2024-04-08'), 28 | ('Grace', 'Smartwatch', 1, 300.00, '2024-04-25'), 29 | ('Hannah', 'Tablet', 2, 480.00, '2024-05-05'), 30 | ('Isaac', 'Laptop', 1, 1250.00, '2024-05-15'), 31 | ('Jack', 'Smartphone', 1, 820.00, '2024-06-01'); 32 | ``` 33 | 34 | # 1. Find customers who purchased at least one item in March 2024 using EXISTS 35 | # 2. Find the product with the highest total sales revenue using a subquery. 36 | # 3. Find the second highest sale amount using a subquery 37 | # 4. Find the total quantity of products sold per month using a subquery 38 | # 5. Find customers who bought same products as another customer using EXISTS 39 | 40 | # 6. Return how many fruits does each person have in individual fruit level 41 | 42 | ```sql 43 | create table Fruits(Name varchar(50), Fruit varchar(50)) 44 | insert into Fruits values ('Francesko', 'Apple'), ('Francesko', 'Apple'), ('Francesko', 'Apple'), ('Francesko', 'Orange'), 45 | ('Francesko', 'Banana'), ('Francesko', 'Orange'), ('Li', 'Apple'), 46 | ('Li', 'Orange'), ('Li', 'Apple'), ('Li', 'Banana'), ('Mario', 'Apple'), ('Mario', 'Apple'), 47 | ('Mario', 'Apple'), ('Mario', 'Banana'), ('Mario', 'Banana'), 48 | ('Mario', 'Orange') 49 | ``` 50 | 51 | **Expected Output** 52 | ``` 53 | +-----------+-------+--------+--------+ 54 | | Name | Apple | Orange | Banana | 55 | +-----------+-------+--------+--------+ 56 | | Francesko | 3 | 2 | 1 | 57 | | Li | 2 | 1 | 1 | 58 | | Mario | 3 | 1 | 2 | 59 | +-----------+-------+--------+--------+ 60 | ``` 61 | 62 | # 7. Return older people in the family with younger ones 63 | ```sql 64 | create table Family(ParentId int, ChildID int) 65 | insert into Family values (1, 2), (2, 3), (3, 4) 66 | ``` 67 | 68 | **1 Oldest person in the family --grandfather** 69 | **2 Father** 70 | **3 Son** 71 | **4 Grandson** 72 | 73 | **Expected output** 74 | ``` 75 | +-----+-----+ 76 | | PID |CHID | 77 | +-----+-----+ 78 | | 1 | 2 | 79 | | 1 | 3 | 80 | | 1 | 4 | 81 | | 2 | 3 | 82 | | 2 | 4 | 83 | | 3 | 4 | 84 | +-----+-----+ 85 | 86 | ``` 87 | 88 | # 8. Write an SQL statement given the following requirements. For every customer that had a delivery to California, provide a result set of the customer orders that were delivered to Texas 89 | ```sql 90 | CREATE TABLE #Orders 91 | ( 92 | CustomerID INTEGER, 93 | OrderID INTEGER, 94 | DeliveryState VARCHAR(100) NOT NULL, 95 | Amount MONEY NOT NULL, 96 | PRIMARY KEY (CustomerID, OrderID) 97 | ); 98 | 99 | 100 | INSERT INTO #Orders (CustomerID, OrderID, DeliveryState, Amount) VALUES 101 | (1001,1,'CA',340),(1001,2,'TX',950),(1001,3,'TX',670), 102 | (1001,4,'TX',860),(2002,5,'WA',320),(3003,6,'CA',650), 103 | (3003,7,'CA',830),(4004,8,'TX',120); 104 | ``` 105 | 106 | --- 107 | # 9. Insert the names of residents if they are missing 108 | 109 | ```sql 110 | create table #residents(resid int identity, fullname varchar(50), address varchar(100)) 111 | 112 | insert into #residents values 113 | ('Dragan', 'city=Bratislava country=Slovakia name=Dragan age=45'), 114 | ('Diogo', 'city=Lisboa country=Portugal age=26'), 115 | ('Celine', 'city=Marseille country=France name=Celine age=21'), 116 | ('Theo', 'city=Milan country=Italy age=28'), 117 | ('Rajabboy', 'city=Tashkent country=Uzbekistan age=22') 118 | ``` 119 | --- 120 | # 10. Write a query to return the route to reach from Tashkent to Khorezm. The result should include the cheapest and the most expensive routes 121 | 122 | ```sql 123 | CREATE TABLE #Routes 124 | ( 125 | RouteID INTEGER NOT NULL, 126 | DepartureCity VARCHAR(30) NOT NULL, 127 | ArrivalCity VARCHAR(30) NOT NULL, 128 | Cost MONEY NOT NULL, 129 | PRIMARY KEY (DepartureCity, ArrivalCity) 130 | ); 131 | 132 | INSERT INTO #Routes (RouteID, DepartureCity, ArrivalCity, Cost) VALUES 133 | (1,'Tashkent','Samarkand',100), 134 | (2,'Samarkand','Bukhoro',200), 135 | (3,'Bukhoro','Khorezm',300), 136 | (4,'Samarkand','Khorezm',400), 137 | (5,'Tashkent','Jizzakh',100), 138 | (6,'Jizzakh','Samarkand',50); 139 | ``` 140 | 141 | **Expected Output** 142 | ``` 143 | | Route |Cost | 144 | |Tashkent - Samarkand - Khorezm | 500 | 145 | |Tashkent - Jizzakh - Samarkand - Bukhoro - Khorezm | 650 | 146 | ``` 147 | --- 148 | # 11. Rank products based on their order of insertion. 149 | 150 | ```sql 151 | CREATE TABLE #RankingPuzzle 152 | ( 153 | ID INT 154 | ,Vals VARCHAR(10) 155 | ) 156 | 157 | 158 | INSERT INTO #RankingPuzzle VALUES 159 | (1,'Product'), 160 | (2,'a'), 161 | (3,'a'), 162 | (4,'a'), 163 | (5,'a'), 164 | (6,'Product'), 165 | (7,'b'), 166 | (8,'b'), 167 | (9,'Product'), 168 | (10,'c') 169 | ``` 170 | 171 | --- 172 | # Question 12 173 | # Find employees whose sales were higher than the average sales in their department 174 | 175 | ```sql 176 | CREATE TABLE #EmployeeSales ( 177 | EmployeeID INT PRIMARY KEY IDENTITY(1,1), 178 | EmployeeName VARCHAR(100), 179 | Department VARCHAR(50), 180 | SalesAmount DECIMAL(10,2), 181 | SalesMonth INT, 182 | SalesYear INT 183 | ); 184 | 185 | INSERT INTO #EmployeeSales (EmployeeName, Department, SalesAmount, SalesMonth, SalesYear) VALUES 186 | ('Alice', 'Electronics', 5000, 1, 2024), 187 | ('Bob', 'Electronics', 7000, 1, 2024), 188 | ('Charlie', 'Furniture', 3000, 1, 2024), 189 | ('David', 'Furniture', 4500, 1, 2024), 190 | ('Eve', 'Clothing', 6000, 1, 2024), 191 | ('Frank', 'Electronics', 8000, 2, 2024), 192 | ('Grace', 'Furniture', 3200, 2, 2024), 193 | ('Hannah', 'Clothing', 7200, 2, 2024), 194 | ('Isaac', 'Electronics', 9100, 3, 2024), 195 | ('Jack', 'Furniture', 5300, 3, 2024), 196 | ('Kevin', 'Clothing', 6800, 3, 2024), 197 | ('Laura', 'Electronics', 6500, 4, 2024), 198 | ('Mia', 'Furniture', 4000, 4, 2024), 199 | ('Nathan', 'Clothing', 7800, 4, 2024); 200 | ``` 201 | 202 | --- 203 | # 13. Find employees who had the highest sales in any given month using EXISTS 204 | --- 205 | # 14. Find employees who made sales in every month using NOT EXISTS 206 | 207 | ```sql 208 | CREATE TABLE Products ( 209 | ProductID INT PRIMARY KEY, 210 | Name VARCHAR(50), 211 | Category VARCHAR(50), 212 | Price DECIMAL(10,2), 213 | Stock INT 214 | ); 215 | 216 | INSERT INTO Products (ProductID, Name, Category, Price, Stock) VALUES 217 | (1, 'Laptop', 'Electronics', 1200.00, 15), 218 | (2, 'Smartphone', 'Electronics', 800.00, 30), 219 | (3, 'Tablet', 'Electronics', 500.00, 25), 220 | (4, 'Headphones', 'Accessories', 150.00, 50), 221 | (5, 'Keyboard', 'Accessories', 100.00, 40), 222 | (6, 'Monitor', 'Electronics', 300.00, 20), 223 | (7, 'Mouse', 'Accessories', 50.00, 60), 224 | (8, 'Chair', 'Furniture', 200.00, 10), 225 | (9, 'Desk', 'Furniture', 400.00, 5), 226 | (10, 'Printer', 'Office Supplies', 250.00, 12), 227 | (11, 'Scanner', 'Office Supplies', 180.00, 8), 228 | (12, 'Notebook', 'Stationery', 10.00, 100), 229 | (13, 'Pen', 'Stationery', 2.00, 500), 230 | (14, 'Backpack', 'Accessories', 80.00, 30), 231 | (15, 'Lamp', 'Furniture', 60.00, 25); 232 | ``` 233 | --- 234 | # 15. Retrieve the names of products that are more expensive than the average price of all products. 235 | --- 236 | # 16. Find the products that have a stock count lower than the highest stock count. 237 | --- 238 | # 17. Get the names of products that belong to the same category as 'Laptop'. 239 | --- 240 | # 18. Retrieve products whose price is greater than the lowest price in the Electronics category. 241 | --- 242 | # 19. Find the products that have a higher price than the average price of their respective category. 243 | ```sql 244 | CREATE TABLE Orders ( 245 | OrderID INT PRIMARY KEY, 246 | ProductID INT, 247 | Quantity INT, 248 | OrderDate DATE, 249 | FOREIGN KEY (ProductID) REFERENCES Products(ProductID) 250 | ); 251 | 252 | INSERT INTO Orders (OrderID, ProductID, Quantity, OrderDate) VALUES 253 | (1, 1, 2, '2024-03-01'), 254 | (2, 3, 5, '2024-03-05'), 255 | (3, 2, 3, '2024-03-07'), 256 | (4, 5, 4, '2024-03-10'), 257 | (5, 8, 1, '2024-03-12'), 258 | (6, 10, 2, '2024-03-15'), 259 | (7, 12, 10, '2024-03-18'), 260 | (8, 7, 6, '2024-03-20'), 261 | (9, 6, 2, '2024-03-22'), 262 | (10, 4, 3, '2024-03-25'), 263 | (11, 9, 2, '2024-03-28'), 264 | (12, 11, 1, '2024-03-30'), 265 | (13, 14, 4, '2024-04-02'), 266 | (14, 15, 5, '2024-04-05'), 267 | (15, 13, 20, '2024-04-08'); 268 | ``` 269 | --- 270 | # 20. Find the products that have been ordered at least once. 271 | --- 272 | # 21. Retrieve the names of products that have been ordered more than the average quantity ordered. 273 | --- 274 | # 22. Find the products that have never been ordered. 275 | --- 276 | # 23. Retrieve the product with the highest total quantity ordered. 277 | --- 278 | -------------------------------------------------------------------------------- /lesson-18/homework/practise.md: -------------------------------------------------------------------------------- 1 | # Lesson-18: View, temp table, variable, functions 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | ---- 11 | 12 | You're working in a database for a Retail Sales System. The database contains the following tables: 13 | ```sql 14 | CREATE TABLE Products ( 15 | ProductID INT PRIMARY KEY, 16 | ProductName VARCHAR(100), 17 | Category VARCHAR(50), 18 | Price DECIMAL(10,2) 19 | ); 20 | 21 | CREATE TABLE Sales ( 22 | SaleID INT PRIMARY KEY, 23 | ProductID INT, 24 | Quantity INT, 25 | SaleDate DATE, 26 | FOREIGN KEY (ProductID) REFERENCES Products(ProductID) 27 | ); 28 | 29 | INSERT INTO Products (ProductID, ProductName, Category, Price) 30 | VALUES 31 | (1, 'Samsung Galaxy S23', 'Electronics', 899.99), 32 | (2, 'Apple iPhone 14', 'Electronics', 999.99), 33 | (3, 'Sony WH-1000XM5 Headphones', 'Electronics', 349.99), 34 | (4, 'Dell XPS 13 Laptop', 'Electronics', 1249.99), 35 | (5, 'Organic Eggs (12 pack)', 'Groceries', 3.49), 36 | (6, 'Whole Milk (1 gallon)', 'Groceries', 2.99), 37 | (7, 'Alpen Cereal (500g)', 'Groceries', 4.75), 38 | (8, 'Extra Virgin Olive Oil (1L)', 'Groceries', 8.99), 39 | (9, 'Mens Cotton T-Shirt', 'Clothing', 12.99), 40 | (10, 'Womens Jeans - Blue', 'Clothing', 39.99), 41 | (11, 'Unisex Hoodie - Grey', 'Clothing', 29.99), 42 | (12, 'Running Shoes - Black', 'Clothing', 59.95), 43 | (13, 'Ceramic Dinner Plate Set (6 pcs)', 'Home & Kitchen', 24.99), 44 | (14, 'Electric Kettle - 1.7L', 'Home & Kitchen', 34.90), 45 | (15, 'Non-stick Frying Pan - 28cm', 'Home & Kitchen', 18.50), 46 | (16, 'Atomic Habits - James Clear', 'Books', 15.20), 47 | (17, 'Deep Work - Cal Newport', 'Books', 14.35), 48 | (18, 'Rich Dad Poor Dad - Robert Kiyosaki', 'Books', 11.99), 49 | (19, 'LEGO City Police Set', 'Toys', 49.99), 50 | (20, 'Rubiks Cube 3x3', 'Toys', 7.99); 51 | 52 | INSERT INTO Sales (SaleID, ProductID, Quantity, SaleDate) 53 | VALUES 54 | (1, 1, 2, '2025-04-01'), 55 | (2, 1, 1, '2025-04-05'), 56 | (3, 2, 1, '2025-04-10'), 57 | (4, 2, 2, '2025-04-15'), 58 | (5, 3, 3, '2025-04-18'), 59 | (6, 3, 1, '2025-04-20'), 60 | (7, 4, 2, '2025-04-21'), 61 | (8, 5, 10, '2025-04-22'), 62 | (9, 6, 5, '2025-04-01'), 63 | (10, 6, 3, '2025-04-11'), 64 | (11, 10, 2, '2025-04-08'), 65 | (12, 12, 1, '2025-04-12'), 66 | (13, 12, 3, '2025-04-14'), 67 | (14, 19, 2, '2025-04-05'), 68 | (15, 20, 4, '2025-04-19'), 69 | (16, 1, 1, '2025-03-15'), 70 | (17, 2, 1, '2025-03-10'), 71 | (18, 5, 5, '2025-02-20'), 72 | (19, 6, 6, '2025-01-18'), 73 | (20, 10, 1, '2024-12-25'), 74 | (21, 1, 1, '2024-04-20'); 75 | ``` 76 | ### 1. Create a temporary table named MonthlySales to store the total quantity sold and total revenue for each product in the current month. 77 | **Return: ProductID, TotalQuantity, TotalRevenue** 78 | 79 | ### 2. Create a view named vw_ProductSalesSummary that returns product info along with total sales quantity across all time. 80 | **Return: ProductID, ProductName, Category, TotalQuantitySold** 81 | 82 | ### 3. Create a function named fn_GetTotalRevenueForProduct(@ProductID INT) 83 | **Return: total revenue for the given product ID** 84 | 85 | ### 4. Create an function fn_GetSalesByCategory(@Category VARCHAR(50)) 86 | **Return: ProductName, TotalQuantity, TotalRevenue for all products in that category.** 87 | 88 | # Now we will move on with 2 Lateral-thinking puzzles (5 and 6th puzzles). Lateral-thinking puzzles are the ones that can’t be solved by straightforward logic — you have to think outside the box. 🔍🧠 89 | 90 | ### 5. You have to create a function that get one argument as input from user and the function should return 'Yes' if the input number is a prime number and 'No' otherwise. You can start it like this: 91 | ```sql 92 | Create function dbo.fn_IsPrime (@Number INT) 93 | Returns ... 94 | ``` 95 | 96 | ``` 97 | This is for those who has no idea about prime numbers: A prime number is a number greater than 1 that has only two divisors: 1 and itself(2, 3, 5, 7 and so on). 98 | ``` 99 | 100 | ### 6. Create a table-valued function named fn_GetNumbersBetween that accepts two integers as input: 101 | ```sql 102 | @Start INT 103 | @End INT 104 | ``` 105 | **The function should return a table with a single column:** 106 | ```sql 107 | | Number | 108 | |--------| 109 | | @Start | 110 | ... 111 | ... 112 | ... 113 | | @end | 114 | ``` 115 | 116 | **It should include all integer values from @Start to @End, inclusive.** 117 | 118 | ### 7. Write a SQL query to return the Nth highest distinct salary from the Employee table. If there are fewer than N distinct salaries, return NULL. 119 | 120 | ### Example 1: 121 | 122 | **Input.Employee table:** 123 | 124 | ``` 125 | | id | salary | 126 | +----+--------+ 127 | | 1 | 100 | 128 | | 2 | 200 | 129 | | 3 | 300 | 130 | ``` 131 | 132 | ### n = 2 133 | 134 | **Output:** 135 | ```sql 136 | | getNthHighestSalary(2) | 137 | ``` 138 | ```sql 139 | | HighestNSalary | 140 | |------------------------| 141 | | 200 | 142 | ``` 143 | 144 | ### Example 2: 145 | 146 | **Input.Employee table:** 147 | 148 | ```sql 149 | | id | salary | 150 | |----|--------| 151 | | 1 | 100 | 152 | ``` 153 | 154 | 155 | ### n = 2 156 | **Output:** 157 | ```sql 158 | | getNthHighestSalary(2) | 159 | ``` 160 | ```sql 161 | | HighestNSalary | 162 | | null | 163 | ``` 164 | 165 | **You can also solve this in Leetcode: https://leetcode.com/problems/nth-highest-salary/description/** 166 | 167 | ### 8. Write a SQL query to find the person who has the most friends. 168 | 169 | **Return: Their id, The total number of friends they have** 170 | 171 | #### Friendship is mutual. For example, if user A sends a request to user B and it's accepted, both A and B are considered friends with each other. The test case is guaranteed to have only one user with the most friends. 172 | 173 | **Input.RequestAccepted table:** 174 | 175 | ``` 176 | | requester_id | accepter_id | accept_date | 177 | +--------------+-------------+-------------+ 178 | | 1 | 2 | 2016/06/03 | 179 | | 1 | 3 | 2016/06/08 | 180 | | 2 | 3 | 2016/06/08 | 181 | | 3 | 4 | 2016/06/09 | 182 | ``` 183 | 184 | **Output:** 185 | ``` 186 | | id | num | 187 | +----+-----+ 188 | | 3 | 3 | 189 | ``` 190 | 191 | **Explanation: The person with id 3 is a friend of people 1, 2, and 4, so he has three friends in total, which is the most number than any others.** 192 | 193 | **You can also solve this in Leetcode: https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/description/?envType=study-plan-v2&envId=top-sql-50** 194 | 195 | ### 9. Create a View for Customer Order Summary. 196 | ```sql 197 | CREATE TABLE Customers ( 198 | customer_id INT PRIMARY KEY, 199 | name VARCHAR(100), 200 | city VARCHAR(50) 201 | ); 202 | 203 | CREATE TABLE Orders ( 204 | order_id INT PRIMARY KEY, 205 | customer_id INT FOREIGN KEY REFERENCES Customers(customer_id), 206 | order_date DATE, 207 | amount DECIMAL(10,2) 208 | ); 209 | 210 | -- Customers 211 | INSERT INTO Customers (customer_id, name, city) 212 | VALUES 213 | (1, 'Alice Smith', 'New York'), 214 | (2, 'Bob Jones', 'Chicago'), 215 | (3, 'Carol White', 'Los Angeles'); 216 | 217 | -- Orders 218 | INSERT INTO Orders (order_id, customer_id, order_date, amount) 219 | VALUES 220 | (101, 1, '2024-12-10', 120.00), 221 | (102, 1, '2024-12-20', 200.00), 222 | (103, 1, '2024-12-30', 220.00), 223 | (104, 2, '2025-01-12', 120.00), 224 | (105, 2, '2025-01-20', 180.00); 225 | ``` 226 | **Create a view called vw_CustomerOrderSummary that returns a summary of customer orders. The view must contain the following columns:** 227 | 228 | > - Column Name | Description 229 | > - customer_id | Unique identifier of the customer 230 | > - name | Full name of the customer 231 | > - total_orders | Total number of orders placed by the customer 232 | > - total_amount | Cumulative amount spent across all orders 233 | > - last_order_date | Date of the most recent order placed by the customer 234 | 235 | ### 10. Write an SQL statement to fill in the missing gaps. You have to write only select statement, no need to modify the table. 236 | 237 | ``` 238 | | RowNumber | Workflow | 239 | |----------------------| 240 | | 1 | Alpha | 241 | | 2 | | 242 | | 3 | | 243 | | 4 | | 244 | | 5 | Bravo | 245 | | 6 | | 246 | | 7 | | 247 | | 8 | | 248 | | 9 | | 249 | | 10 | Charlie | 250 | | 11 | | 251 | | 12 | | 252 | ``` 253 | 254 | **Here is the expected output.** 255 | ``` 256 | | RowNumber | Workflow | 257 | |----------------------| 258 | | 1 | Alpha | 259 | | 2 | Alpha | 260 | | 3 | Alpha | 261 | | 4 | Alpha | 262 | | 5 | Bravo | 263 | | 6 | Bravo | 264 | | 7 | Bravo | 265 | | 8 | Bravo | 266 | | 9 | Bravo | 267 | | 10 | Charlie | 268 | | 11 | Charlie | 269 | | 12 | Charlie | 270 | ``` 271 | 272 | ```sql 273 | DROP TABLE IF EXISTS Gaps; 274 | 275 | CREATE TABLE Gaps 276 | ( 277 | RowNumber INTEGER PRIMARY KEY, 278 | TestCase VARCHAR(100) NULL 279 | ); 280 | 281 | INSERT INTO Gaps (RowNumber, TestCase) VALUES 282 | (1,'Alpha'),(2,NULL),(3,NULL),(4,NULL), 283 | (5,'Bravo'),(6,NULL),(7,NULL),(8,NULL),(9,NULL),(10,'Charlie'), (11, NULL), (12, NULL) 284 | ``` 285 | -------------------------------------------------------------------------------- /lesson-23/homework/homework.md: -------------------------------------------------------------------------------- 1 | 2 | ## SQL Puzzle Questions with Input and Output Tables 3 | 4 | ### Puzzle 1: In this puzzle you have to extract the month from the dt column and then append zero single digit month if any. Please check out sample input and expected output. 5 | 6 | **Input Table: Dates** 7 | ```sql 8 | CREATE TABLE Dates ( 9 | Id INT, 10 | Dt DATETIME 11 | ); 12 | INSERT INTO Dates VALUES 13 | (1,'2018-04-06 11:06:43.020'), 14 | (2,'2017-12-06 11:06:43.020'), 15 | (3,'2016-01-06 11:06:43.020'), 16 | (4,'2015-11-06 11:06:43.020'), 17 | (5,'2014-10-06 11:06:43.020'); 18 | ``` 19 | 20 | **Sample Input** 21 | ``` 22 | | Id | Dt | 23 | |----|-----------------------------| 24 | | 1 | 2018-04-06 11:06:43.020 | 25 | | 2 | 2017-12-06 11:06:43.020 | 26 | | 3 | 2016-01-06 11:06:43.020 | 27 | | 4 | 2015-11-06 11:06:43.020 | 28 | | 5 | 2014-10-06 11:06:43.020 | 29 | ``` 30 | 31 | **Expected Output:** 32 | ``` 33 | | Id | Dt | MonthPrefixedWithZero | 34 | |----|--------------------------|------------------------| 35 | | 1 | 2018-04-06 11:06:43.020 | 04 | 36 | | 2 | 2017-12-06 11:06:43.020 | 12 | 37 | | 3 | 2016-01-06 11:06:43.020 | 01 | 38 | | 4 | 2015-11-06 11:06:43.020 | 11 | 39 | | 5 | 2014-10-06 11:06:43.020 | 10 | 40 | 41 | --- 42 | ``` 43 | ### Puzzle 2: In this puzzle you have to find out the unique Ids present in the table. You also have to find out the SUM of Max values of vals columns for each Id and RId. For more details please see the sample input and expected output. 44 | 45 | **Input Table: MyTabel** 46 | ```sql 47 | CREATE TABLE MyTabel ( 48 | Id INT, 49 | rID INT, 50 | Vals INT 51 | ); 52 | INSERT INTO MyTabel VALUES 53 | (121, 9, 1), (121, 9, 8), 54 | (122, 9, 14), (122, 9, 0), (122, 9, 1), 55 | (123, 9, 1), (123, 9, 2), (123, 9, 10); 56 | ``` 57 | 58 | 59 | **Sample Input:** 60 | ``` 61 | | Id | rID | Vals | 62 | |-----|-----|------| 63 | | 121 | 9 | 1 | 64 | | 121 | 9 | 8 | 65 | | 122 | 9 | 14 | 66 | | 122 | 9 | 0 | 67 | | 122 | 9 | 1 | 68 | | 123 | 9 | 1 | 69 | | 123 | 9 | 2 | 70 | | 123 | 9 | 10 | 71 | ``` 72 | 73 | **Expected Output:** 74 | ``` 75 | | Distinct_Ids | rID | TotalOfMaxVals | 76 | |--------------|-----|----------------| 77 | | 3 | 9 | 32 | 78 | ``` 79 | 80 | --- 81 | 82 | ### Puzzle 3: In this puzzle you have to get records with at least 6 characters and maximum 10 characters. Please see the sample input and expected output. 83 | 84 | **Input Table: TestFixLengths** 85 | ```sql 86 | CREATE TABLE TestFixLengths ( 87 | Id INT, 88 | Vals VARCHAR(100) 89 | ); 90 | INSERT INTO TestFixLengths VALUES 91 | (1,'11111111'), (2,'123456'), (2,'1234567'), 92 | (2,'1234567890'), (5,''), (6,NULL), 93 | (7,'123456789012345'); 94 | ``` 95 | **Sample Input** 96 | ``` 97 | | Id | Vals | 98 | +----+------------------+ 99 | | 1 | 11111111 | 100 | | 2 | 123456 | 101 | | 2 | 123467 | 102 | | 2 | 1234567890 | 103 | | 5 | | 104 | | 6 | NULL | 105 | | 7 | 123456789012345 | 106 | ``` 107 | 108 | **Expected Output** 109 | ``` 110 | | Id | Vals | 111 | +----+--------------+ 112 | | 1 | 11111111 | 113 | | 2 | 123456 | 114 | | 2 | 123467 | 115 | | 2 | 1234567890 | 116 | ``` 117 | 118 | --- 119 | 120 | ### Puzzle 4: In this puzzle you have to find the maximum value for each Id and then get the Item for that Id and Maximum value. Please check out sample input and expected output. 121 | 122 | **Input Table: TestMaximum** 123 | ```sql 124 | CREATE TABLE TestMaximum ( 125 | ID INT, 126 | Item VARCHAR(20), 127 | Vals INT 128 | ); 129 | INSERT INTO TestMaximum VALUES 130 | (1, 'a1',15), (1, 'a2',20), (1, 'a3',90), 131 | (2, 'q1',10), (2, 'q2',40), (2, 'q3',60), (2, 'q4',30), 132 | (3, 'q5',20); 133 | ``` 134 | 135 | **Sample Input** 136 | ``` 137 | | ID | Item | Vals | 138 | +----+-------+------+ 139 | | 1 | a1 | 15 | 140 | | 1 | a2 | 20 | 141 | | 1 | a3 | 90 | 142 | | 1 | q1 | 10 | 143 | | 2 | q2 | 40 | 144 | | 2 | q3 | 60 | 145 | | 2 | q4 | 30 | 146 | | 3 | q5 | 20 | 147 | ``` 148 | 149 | **Expected Output** 150 | ``` 151 | | ID | Item | Vals | 152 | +----+-------+------+ 153 | | 2 | q3 | 60 | 154 | | 1 | a3 | 90 | 155 | | 3 | q5 | 20 | 156 | ``` 157 | 158 | --- 159 | 160 | ### Puzzle 5: In this puzzle you have to first find the maximum value for each Id and DetailedNumber, and then Sum the data using Id only. Please check out sample input and expected output. 161 | 162 | **Input Table: SumOfMax** 163 | ```sql 164 | CREATE TABLE SumOfMax ( 165 | DetailedNumber INT, 166 | Vals INT, 167 | Id INT 168 | ); 169 | INSERT INTO SumOfMax VALUES 170 | (1,5,101), (1,4,101), (2,6,101), (2,3,101), 171 | (3,3,102), (4,2,102), (4,3,102); 172 | ``` 173 | 174 | **Sample Input** 175 | ``` 176 | | DetailedNumber | Vals | Id | 177 | +----------------+------+-----+ 178 | | 1 | 5 | 101 | 179 | | 1 | 4 | 101 | 180 | | 2 | 6 | 101 | 181 | | 2 | 3 | 101 | 182 | | 3 | 3 | 102 | 183 | | 4 | 2 | 102 | 184 | | 4 | 3 | 102 | 185 | ``` 186 | 187 | **Expected Output** 188 | ``` 189 | | Id | SumofMax| 190 | +-----+---------+ 191 | | 101 | 11 | 192 | | 102 | 6 | 193 | ``` 194 | 195 | --- 196 | ### Puzzle 6: In this puzzle you have to find difference between a and b column between each row and if the difference is not equal to 0 then show the difference i.e. a – b otherwise 0. Now you need to replace this zero with blank.Please check the sample input and the expected output. 197 | 198 | 199 | **Input Table: TheZeroPuzzle** 200 | ```sql 201 | CREATE TABLE TheZeroPuzzle ( 202 | Id INT, 203 | a INT, 204 | b INT 205 | ); 206 | INSERT INTO TheZeroPuzzle VALUES 207 | (1,10,4), (2,10,10), (3,1, 10000000), (4,15,15); 208 | ``` 209 | 210 | **Sample Input** 211 | ``` 212 | | Id | a | b | 213 | +----+----+----------+ 214 | | 1 | 10 | 4 | 215 | | 2 | 10 | 10 | 216 | | 3 | 1 | 10000000 | 217 | | 4 | 15 | 15 | 218 | ``` 219 | 220 | **Expected Output** 221 | ``` 222 | | Id | a | b | OUTPUT | 223 | +----+----+----------+---------+ 224 | | 1 | 10 | 4 | 6 | 225 | | 2 | 10 | 10 | | 226 | | 3 | 1 | 10000000 | -9999999| 227 | | 4 | 15 | 15 | | 228 | ``` 229 | --- 230 | 231 | 232 | ```sql 233 | CREATE TABLE Sales ( 234 | SaleID INT PRIMARY KEY IDENTITY(1,1), 235 | Product VARCHAR(50), 236 | Category VARCHAR(50), 237 | QuantitySold INT, 238 | UnitPrice DECIMAL(10,2), 239 | SaleDate DATE, 240 | Region VARCHAR(50), 241 | CustomerID INT, 242 | FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) 243 | ); 244 | 245 | INSERT INTO Sales (Product, Category, QuantitySold, UnitPrice, SaleDate, Region, CustomerID) 246 | VALUES 247 | ('Laptop', 'Electronics', 10, 800.00, '2024-01-01', 'North', 1), 248 | ('Smartphone', 'Electronics', 15, 500.00, '2024-01-02', 'North', 2), 249 | ('Tablet', 'Electronics', 8, 300.00, '2024-01-03', 'East', 3), 250 | ('Headphones', 'Electronics', 25, 100.00, '2024-01-04', 'West', 4), 251 | ('TV', 'Electronics', 5, 1200.00, '2024-01-05', 'South', 5), 252 | ('Refrigerator', 'Appliances', 3, 1500.00, '2024-01-06', 'South', 6), 253 | ('Microwave', 'Appliances', 7, 200.00, '2024-01-07', 'East', 7), 254 | ('Washing Machine', 'Appliances', 4, 1000.00, '2024-01-08', 'North', 8), 255 | ('Oven', 'Appliances', 6, 700.00, '2024-01-09', 'West', 9), 256 | ('Smartwatch', 'Electronics', 12, 250.00, '2024-01-10', 'East', 10), 257 | ('Vacuum Cleaner', 'Appliances', 5, 400.00, '2024-01-11', 'South', 1), 258 | ('Gaming Console', 'Electronics', 9, 450.00, '2024-01-12', 'North', 2), 259 | ('Monitor', 'Electronics', 14, 300.00, '2024-01-13', 'West', 3), 260 | ('Keyboard', 'Electronics', 20, 50.00, '2024-01-14', 'South', 4), 261 | ('Mouse', 'Electronics', 30, 25.00, '2024-01-15', 'East', 5), 262 | ('Blender', 'Appliances', 10, 150.00, '2024-01-16', 'North', 6), 263 | ('Fan', 'Appliances', 12, 75.00, '2024-01-17', 'South', 7), 264 | ('Heater', 'Appliances', 8, 120.00, '2024-01-18', 'East', 8), 265 | ('Air Conditioner', 'Appliances', 2, 2000.00, '2024-01-19', 'West', 9), 266 | ('Camera', 'Electronics', 7, 900.00, '2024-01-20', 'North', 10); 267 | 268 | 269 | ``` 270 | 7. What is the total revenue generated from all sales? 271 | 8. What is the average unit price of products? 272 | 9. How many sales transactions were recorded? 273 | 10. What is the highest number of units sold in a single transaction? 274 | 11. How many products were sold in each category? 275 | 12. What is the total revenue for each region? 276 | 13. Which product generated the highest total revenue? 277 | 14. Compute the running total of revenue ordered by sale date. 278 | 15. How much does each category contribute to total sales revenue? 279 | 280 | --- 281 | 282 | **Input Table: Customers** 283 | ```sql 284 | CREATE TABLE Customers ( 285 | CustomerID INT PRIMARY KEY IDENTITY(1,1), 286 | CustomerName VARCHAR(100), 287 | Region VARCHAR(50), 288 | JoinDate DATE 289 | ); 290 | 291 | INSERT INTO Customers (CustomerName, Region, JoinDate) 292 | VALUES 293 | ('John Doe', 'North', '2022-03-01'), 294 | ('Jane Smith', 'West', '2023-06-15'), 295 | ('Emily Davis', 'East', '2021-11-20'), 296 | ('Michael Brown', 'South', '2023-01-10'), 297 | ('Sarah Wilson', 'North', '2022-07-25'), 298 | ('David Martinez', 'East', '2023-04-30'), 299 | ('Laura Johnson', 'West', '2022-09-14'), 300 | ('Kevin Anderson', 'South', '2021-12-05'), 301 | ('Sophia Moore', 'North', '2023-02-17'), 302 | ('Daniel Garcia', 'East', '2022-08-22'); 303 | 304 | ``` 305 | 306 | 17. Show all sales along with the corresponding customer names 307 | 18. List customers who have not made any purchases 308 | 19. Compute total revenue generated from each customer 309 | 20. Find the customer who has contributed the most revenue 310 | 21. Calculate the total sales per customer 311 | 312 | --- 313 | 314 | **Input Table: Products** 315 | ```sql 316 | CREATE TABLE Products ( 317 | ProductID INT PRIMARY KEY IDENTITY(1,1), 318 | ProductName VARCHAR(50), 319 | Category VARCHAR(50), 320 | CostPrice DECIMAL(10,2), 321 | SellingPrice DECIMAL(10,2) 322 | ); 323 | INSERT INTO Products (ProductName, Category, CostPrice, SellingPrice) 324 | VALUES 325 | ('Laptop', 'Electronics', 600.00, 800.00), 326 | ('Smartphone', 'Electronics', 350.00, 500.00), 327 | ('Tablet', 'Electronics', 200.00, 300.00), 328 | ('Headphones', 'Electronics', 50.00, 100.00), 329 | ('TV', 'Electronics', 900.00, 1200.00), 330 | ('Refrigerator', 'Appliances', 1100.00, 1500.00), 331 | ('Microwave', 'Appliances', 120.00, 200.00), 332 | ('Washing Machine', 'Appliances', 700.00, 1000.00), 333 | ('Oven', 'Appliances', 500.00, 700.00), 334 | ('Gaming Console', 'Electronics', 320.00, 450.00); 335 | ``` 336 | 337 | 22. List all products that have been sold at least once 338 | 23. Find the most expensive product in the Products table 339 | 24. Find all products where the selling price is higher than the average selling price in their category 340 | -------------------------------------------------------------------------------- /lesson-17/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson-17: Practice 2 | 3 | > **Notes before doing the tasks:** 4 | > - Tasks should be solved using **SQL Server**. 5 | > - Case insensitivity applies. 6 | > - Alias names do not affect the score. 7 | > - Scoring is based on the **correct output**. 8 | > - One correct solution is sufficient. 9 | 10 | --- 11 | 12 | ### 1. You must provide a report of all distributors and their sales by region. If a distributor did not have any sales for a region, rovide a zero-dollar value for that day. Assume there is at least one sale for each region 13 | 14 | **SQL Setup:** 15 | ```sql 16 | DROP TABLE IF EXISTS #RegionSales; 17 | GO 18 | CREATE TABLE #RegionSales ( 19 | Region VARCHAR(100), 20 | Distributor VARCHAR(100), 21 | Sales INTEGER NOT NULL, 22 | PRIMARY KEY (Region, Distributor) 23 | ); 24 | GO 25 | INSERT INTO #RegionSales (Region, Distributor, Sales) VALUES 26 | ('North','ACE',10), ('South','ACE',67), ('East','ACE',54), 27 | ('North','ACME',65), ('South','ACME',9), ('East','ACME',1), ('West','ACME',7), 28 | ('North','Direct Parts',8), ('South','Direct Parts',7), ('West','Direct Parts',12); 29 | ``` 30 | 31 | 32 | **Input:** 33 | ``` 34 | |Region |Distributor | Sales | 35 | |-------------|---------------|-------- 36 | |North |ACE | 10 | 37 | |South |ACE | 67 | 38 | |East |ACE | 54 | 39 | |North |Direct Parts | 8 | 40 | |South |Direct Parts | 7 | 41 | |West |Direct Parts | 12 | 42 | |North |ACME | 65 | 43 | |South |ACME | 9 | 44 | |East |ACME | 1 | 45 | |West |ACME | 7 | 46 | ``` 47 | 48 | 49 | 50 | **Expected Output:** 51 | ``` 52 | |Region |Distributor | Sales | 53 | |-------|--------------|-------| 54 | |North |ACE | 10 | 55 | |South |ACE | 67 | 56 | |East |ACE | 54 | 57 | |West |ACE | 0 | 58 | |North |Direct Parts | 8 | 59 | |South |Direct Parts | 7 | 60 | |East |Direct Parts | 0 | 61 | |West |Direct Parts | 12 | 62 | |North |ACME | 65 | 63 | |South |ACME | 9 | 64 | |East |ACME | 1 | 65 | |West |ACME | 7 | 66 | ``` 67 | 68 | --- 69 | 70 | ### 2. Find managers with at least five direct reports 71 | 72 | **SQL Setup:** 73 | ```sql 74 | CREATE TABLE Employee (id INT, name VARCHAR(255), department VARCHAR(255), managerId INT); 75 | TRUNCATE TABLE Employee; 76 | INSERT INTO Employee VALUES 77 | (101, 'John', 'A', NULL), (102, 'Dan', 'A', 101), (103, 'James', 'A', 101), 78 | (104, 'Amy', 'A', 101), (105, 'Anne', 'A', 101), (106, 'Ron', 'B', 101); 79 | ``` 80 | 81 | **Input:** 82 | ``` 83 | | id | name | department | managerId | 84 | +-----+-------+------------+-----------+ 85 | | 101 | John | A | null | 86 | | 102 | Dan | A | 101 | 87 | | 103 | James | A | 101 | 88 | | 104 | Amy | A | 101 | 89 | | 105 | Anne | A | 101 | 90 | | 106 | Ron | B | 101 | 91 | ``` 92 | 93 | **Expected Output:** 94 | ``` 95 | +------+ 96 | | name | 97 | +------+ 98 | | John | 99 | +------+ 100 | ``` 101 | 102 | ``` 103 | You cal also solve this puzzle in Leetcode: https://leetcode.com/problems/managers-with-at-least-5-direct-reports/description/?envType=study-plan-v2&envId=top-sql-50 104 | ``` 105 | 106 | --- 107 | 108 | ### 3. Write a solution to get the names of products that have at least 100 units ordered in February 2020 and their amount. 109 | 110 | **SQL Setup:** 111 | ```sql 112 | CREATE TABLE Products (product_id INT, product_name VARCHAR(40), product_category VARCHAR(40)); 113 | CREATE TABLE Orders (product_id INT, order_date DATE, unit INT); 114 | TRUNCATE TABLE Products; 115 | INSERT INTO Products VALUES 116 | (1, 'Leetcode Solutions', 'Book'), 117 | (2, 'Jewels of Stringology', 'Book'), 118 | (3, 'HP', 'Laptop'), (4, 'Lenovo', 'Laptop'), (5, 'Leetcode Kit', 'T-shirt'); 119 | TRUNCATE TABLE Orders; 120 | INSERT INTO Orders VALUES 121 | (1,'2020-02-05',60),(1,'2020-02-10',70), 122 | (2,'2020-01-18',30),(2,'2020-02-11',80), 123 | (3,'2020-02-17',2),(3,'2020-02-24',3), 124 | (4,'2020-03-01',20),(4,'2020-03-04',30),(4,'2020-03-04',60), 125 | (5,'2020-02-25',50),(5,'2020-02-27',50),(5,'2020-03-01',50); 126 | ``` 127 | 128 | **Input:** 129 | ``` 130 | | product_id | product_name | product_category | 131 | +-------------+-----------------------+------------------+ 132 | | 1 | Leetcode Solutions | Book | 133 | | 2 | Jewels of Stringology | Book | 134 | | 3 | HP | Laptop | 135 | | 4 | Lenovo | Laptop | 136 | | 5 | Leetcode Kit | T-shirt | 137 | ``` 138 | 139 | 140 | 141 | **Expected Output:** 142 | ``` 143 | | product_name | unit | 144 | +--------------------+-------+ 145 | | Leetcode Solutions | 130 | 146 | | Leetcode Kit | 100 | 147 | ``` 148 | 149 | --- 150 | 151 | ### 4. Write an SQL statement that returns the vendor from which each customer has placed the most orders 152 | 153 | 154 | **SQL Setup:** 155 | ```sql 156 | DROP TABLE IF EXISTS Orders; 157 | CREATE TABLE Orders ( 158 | OrderID INTEGER PRIMARY KEY, 159 | CustomerID INTEGER NOT NULL, 160 | [Count] MONEY NOT NULL, 161 | Vendor VARCHAR(100) NOT NULL 162 | ); 163 | INSERT INTO Orders VALUES 164 | (1,1001,12,'Direct Parts'), (2,1001,54,'Direct Parts'), (3,1001,32,'ACME'), 165 | (4,2002,7,'ACME'), (5,2002,16,'ACME'), (6,2002,5,'Direct Parts'); 166 | ``` 167 | 168 | **Input:** 169 | ``` 170 | |Order ID | Customer ID | Order Count| Vendor | 171 | --------------------------------------------------------- 172 | |Ord195342 | 1001 | 12 | Direct Parts | 173 | |Ord245532 | 1001 | 54 | Direct Parts | 174 | |Ord344394 | 1001 | 32 | ACME | 175 | |Ord442423 | 2002 | 7 | ACME | 176 | |Ord524232 | 2002 | 16 | ACME | 177 | |Ord645363 | 2002 | 5 | Direct Parts | 178 | ``` 179 | 180 | **Expected Output:** 181 | ``` 182 | | CustomerID | Vendor | 183 | |------------|--------------| 184 | | 1001 | Direct Parts | 185 | | 2002 | ACME | 186 | ``` 187 | 188 | --- 189 | 190 | ### 5. You will be given a number as a variable called @Check_Prime check if this number is prime then return 'This number is prime' else eturn 'This number is not prime' 191 | 192 | **Example Input:** 193 | ```sql 194 | DECLARE @Check_Prime INT = 91; 195 | -- Your WHILE-based SQL logic here 196 | ``` 197 | 198 | **Expected Output:** 199 | ``` 200 | This number is not prime(or "This number is prime") 201 | ``` 202 | 203 | --- 204 | 205 | ### 6. Write an SQL query to return the number of locations,in which location most signals sent, and total number of signal for each device from the given table. 206 | 207 | **SQL Setup:** 208 | ```sql 209 | CREATE TABLE Device( 210 | Device_id INT, 211 | Locations VARCHAR(25) 212 | ); 213 | INSERT INTO Device VALUES 214 | (12,'Bangalore'), (12,'Bangalore'), (12,'Bangalore'), (12,'Bangalore'), 215 | (12,'Hosur'), (12,'Hosur'), 216 | (13,'Hyderabad'), (13,'Hyderabad'), (13,'Secunderabad'), 217 | (13,'Secunderabad'), (13,'Secunderabad'); 218 | ``` 219 | 220 | 221 | 222 | 223 | 224 | **Expected Output:** 225 | ``` 226 | | Device_id | no_of_location | max_signal_location | no_of_signals | 227 | |-----------|----------------|---------------------|---------------| 228 | | 12 | 2 | Bangalore | 6 | 229 | | 13 | 2 | Secunderabad | 5 | 230 | ``` 231 | 232 | --- 233 | 234 | ### 7. Write a SQL to find all Employees who earn more than the average salary in their corresponding department. Return EmpID, EmpName,Salary in your output 235 | 236 | **SQL Setup:** 237 | ```sql 238 | CREATE TABLE Employee ( 239 | EmpID INT, 240 | EmpName VARCHAR(30), 241 | Salary FLOAT, 242 | DeptID INT 243 | ); 244 | INSERT INTO Employee VALUES 245 | (1001,'Mark',60000,2), (1002,'Antony',40000,2), (1003,'Andrew',15000,1), 246 | (1004,'Peter',35000,1), (1005,'John',55000,1), (1006,'Albert',25000,3), (1007,'Donald',35000,3); 247 | ``` 248 | 249 | **Expected Output:** 250 | ``` 251 | | EmpID | EmpName | Salary | 252 | |-------|---------|--------| 253 | | 1001 | Mark | 60000 | 254 | | 1004 | Peter | 35000 | 255 | | 1005 | John | 55000 | 256 | | 1007 | Donald | 35000 | 257 | ``` 258 | 259 | --- 260 | 261 | ### 8. You are part of an office lottery pool where you keep a table of the winning lottery numbers along with a table of each ticket’s chosen numbers. If a ticket has some but not all the winning numbers, you win $10. If a ticket has all the winning numbers, you win $100. Calculate the total winnings for today’s drawing. 262 | 263 | **Winning Numbers:** 264 | ``` 265 | |Number| 266 | -------- 267 | | 25 | 268 | | 45 | 269 | | 78 | 270 | 271 | ``` 272 | 273 | 274 | **Tickets:** 275 | ``` 276 | | Ticket ID | Number | 277 | |-----------|--------| 278 | | A23423 | 25 | 279 | | A23423 | 45 | 280 | | A23423 | 78 | 281 | | B35643 | 25 | 282 | | B35643 | 45 | 283 | | B35643 | 98 | 284 | | C98787 | 67 | 285 | | C98787 | 86 | 286 | | C98787 | 91 | 287 | 288 | -- Step 1: Create the table 289 | CREATE TABLE Numbers ( 290 | Number INT 291 | ); 292 | 293 | -- Step 2: Insert values into the table 294 | INSERT INTO Numbers (Number) 295 | VALUES 296 | (25), 297 | (45), 298 | (78); 299 | 300 | 301 | -- Step 1: Create the Tickets table 302 | CREATE TABLE Tickets ( 303 | TicketID VARCHAR(10), 304 | Number INT 305 | ); 306 | 307 | -- Step 2: Insert the data into the table 308 | INSERT INTO Tickets (TicketID, Number) 309 | VALUES 310 | ('A23423', 25), 311 | ('A23423', 45), 312 | ('A23423', 78), 313 | ('B35643', 25), 314 | ('B35643', 45), 315 | ('B35643', 98), 316 | ('C98787', 67), 317 | ('C98787', 86), 318 | ('C98787', 91); 319 | 320 | 321 | ``` 322 | 323 | **Expected Output would be $110, as you have one winning ticket, and one ticket that has some but not all the winning numbers.** 324 | 325 | --- 326 | 327 | ### 9. The Spending table keeps the logs of the spendings history of users that make purchases from an online shopping website which has a desktop and a mobile devices. 328 | 329 | ## Write an SQL query to find the total number of users and the total amount spent using mobile only, desktop only and both mobile and desktop together for each date. 330 | 331 | **SQL Setup:** 332 | ```sql 333 | CREATE TABLE Spending ( 334 | User_id INT, 335 | Spend_date DATE, 336 | Platform VARCHAR(10), 337 | Amount INT 338 | ); 339 | INSERT INTO Spending VALUES 340 | (1,'2019-07-01','Mobile',100), 341 | (1,'2019-07-01','Desktop',100), 342 | (2,'2019-07-01','Mobile',100), 343 | (2,'2019-07-02','Mobile',100), 344 | (3,'2019-07-01','Desktop',100), 345 | (3,'2019-07-02','Desktop',100); 346 | ``` 347 | 348 | **Expected Output:** 349 | ``` 350 | | Row | Spend_date | Platform | Total_Amount | Total_users | 351 | |-----|------------|----------|--------------|-------------| 352 | | 1 | 2019-07-01 | Mobile | 200 | 2 | 353 | | 2 | 2019-07-01 | Desktop | 200 | 2 | 354 | | 3 | 2019-07-01 | Both | 400 | 3 | 355 | | 4 | 2019-07-02 | Mobile | 100 | 1 | 356 | | 5 | 2019-07-02 | Desktop | 100 | 1 | 357 | | 6 | 2019-07-02 | Both | 200 | 2 | 358 | ``` 359 | 360 | --- 361 | 362 | 363 | ### 10. Write an SQL Statement to de-group the following data. 364 | 365 | **Input Table: 'Grouped'** 366 | ``` 367 | |Product |Quantity| 368 | -------------------- 369 | |Pencil | 3 | 370 | |Eraser | 4 | 371 | |Notebook | 2 | 372 | ``` 373 | 374 | **Expected Output:** 375 | ``` 376 | |Product |Quantity| 377 | -------------------- 378 | |Pencil | 1 | 379 | |Pencil | 1 | 380 | |Pencil | 1 | 381 | |Eraser | 1 | 382 | |Eraser | 1 | 383 | |Eraser | 1 | 384 | |Eraser | 1 | 385 | |Notebook | 1 | 386 | |Notebook | 1 | 387 | ``` 388 | 389 | **SQL Setup:** 390 | ```sql 391 | DROP TABLE IF EXISTS Grouped; 392 | CREATE TABLE Grouped 393 | ( 394 | Product VARCHAR(100) PRIMARY KEY, 395 | Quantity INTEGER NOT NULL 396 | ); 397 | INSERT INTO Grouped (Product, Quantity) VALUES 398 | ('Pencil', 3), ('Eraser', 4), ('Notebook', 2); 399 | ``` 400 | 401 | --- 402 | 403 | 404 | 405 | -------------------------------------------------------------------------------- /lesson-12/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson-12: Practice 2 | 3 | **You can find tsql2012 database in here: https://gist.github.com/alejotima/cac70484db23834591b142ad07e79e67** 4 | 5 | > **Notes before doing the tasks:** 6 | > - Tasks should be solved using **SQL Server**. 7 | > - Case insensitivity applies. 8 | > - Alias names do not affect the score. 9 | > - Scoring is based on the **correct output**. 10 | > - One correct solution is sufficient. 11 | 12 | # 1. Combine Two Tables 13 | 14 | **Table: Person** 15 | ``` 16 | | Column Name | Type | 17 | +-------------+---------+ 18 | | personId | int | 19 | | lastName | varchar | 20 | | firstName | varchar | 21 | ``` 22 | **personId is the primary key (column with unique values) for this table.** 23 | **This table contains information about the ID of some persons and their first and last names.** 24 | 25 | **Table: Address** 26 | 27 | ``` 28 | | Column Name | Type | 29 | +-------------+---------+ 30 | | addressId | int | 31 | | personId | int | 32 | | city | varchar | 33 | | state | varchar | 34 | ``` 35 | 36 | **AddressId is the primary key (column with unique values) for this table.** 37 | **Each row of this table contains information about the city and state of one person with ID = PersonId.** 38 | 39 | 40 | **Write a solution to report the first name, last name, city, and state of each person in the Person table.** 41 | **If the address of a personId is not present in the Address table, report null instead.** 42 | 43 | **The result format is in the following example.** 44 | 45 | **Input: Person table:** 46 | ``` 47 | | personId | lastName | firstName | 48 | +----------+----------+-----------+ 49 | | 1 | Wang | Allen | 50 | | 2 | Alice | Bob | 51 | ``` 52 | 53 | **Address table:** 54 | ``` 55 | | addressId | personId | city | state | 56 | +-----------+----------+---------------+------------+ 57 | | 1 | 2 | New York City | New York | 58 | | 2 | 3 | Leetcode | California | 59 | ``` 60 | 61 | **Output:** 62 | ``` 63 | 64 | | firstName | lastName | city | state | 65 | +-----------+----------+---------------+----------+ 66 | | Allen | Wang | Null | Null | 67 | | Bob | Alice | New York City | New York | 68 | ``` 69 | 70 | # Explanation: 71 | There is no address in the address table for the personId = 1 so we return null in their city and state. 72 | addressId = 1 contains information about the address of personId = 2. 73 | 74 | --- 75 | ```sql 76 | Create table Person (personId int, firstName varchar(255), lastName varchar(255)) 77 | Create table Address (addressId int, personId int, city varchar(255), state varchar(255)) 78 | Truncate table Person 79 | insert into Person (personId, lastName, firstName) values ('1', 'Wang', 'Allen') 80 | insert into Person (personId, lastName, firstName) values ('2', 'Alice', 'Bob') 81 | Truncate table Address 82 | insert into Address (addressId, personId, city, state) values ('1', '2', 'New York City', 'New York') 83 | insert into Address (addressId, personId, city, state) values ('2', '3', 'Leetcode', 'California') 84 | ``` 85 | 86 | --- 87 | # 2. Employees Earning More Than Their Managers 88 | --- 89 | **Table: Employee** 90 | ``` 91 | | Column Name | Type | 92 | +-------------+---------+ 93 | | id | int | 94 | | name | varchar | 95 | | salary | int | 96 | | managerId | int | 97 | ``` 98 | 99 | **Id is the primary key (column with unique values) for this table.** 100 | **Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.** 101 | 102 | --- 103 | 104 | **Write a solution to find the employees who earn more than their managers.** 105 | 106 | The result format is in the following example. 107 | 108 | **Input:** 109 | **Employee table:** 110 | ``` 111 | | id | name | salary | managerId | 112 | +----+-------+--------+-----------+ 113 | | 1 | Joe | 70000 | 3 | 114 | | 2 | Henry | 80000 | 4 | 115 | | 3 | Sam | 60000 | Null | 116 | | 4 | Max | 90000 | Null | 117 | ``` 118 | 119 | **Output:** 120 | ``` 121 | | Employee | 122 | +----------+ 123 | | Joe | 124 | ``` 125 | 126 | # Explanation: Joe is the only employee who earns more than his manager. 127 | 128 | ```sql 129 | Create table Employee (id int, name varchar(255), salary int, managerId int) 130 | Truncate table Employee 131 | insert into Employee (id, name, salary, managerId) values ('1', 'Joe', '70000', '3') 132 | insert into Employee (id, name, salary, managerId) values ('2', 'Henry', '80000', '4') 133 | insert into Employee (id, name, salary, managerId) values ('3', 'Sam', '60000', NULL) 134 | insert into Employee (id, name, salary, managerId) values ('4', 'Max', '90000', NULL) 135 | ``` 136 | 137 | --- 138 | 139 | # 3. Duplicate Emails 140 | ``` 141 | | Column Name | Type | 142 | +-------------+---------+ 143 | | id | int | 144 | | email | varchar | 145 | ``` 146 | 147 | 148 | **Id is the primary key (column with unique values) for this table.** 149 | **Each row of this table contains an email. The emails will not contain uppercase letters.** 150 | 151 | --- 152 | 153 | **Write a solution to report all the duplicate emails. Note that it''s guaranteed that the email field is not NULL.** 154 | 155 | **The result format is in the following example.** 156 | 157 | --- 158 | 159 | **Input:** 160 | **Person table:** 161 | ``` 162 | | id | email | 163 | +----+---------+ 164 | | 1 | a@b.com | 165 | | 2 | c@d.com | 166 | | 3 | a@b.com | 167 | ``` 168 | 169 | **Output:** 170 | ``` 171 | | Email | 172 | +---------+ 173 | | a@b.com | 174 | ``` 175 | 176 | # Explanation: a@b.com is repeated two times. 177 | 178 | 179 | ----------------------------------------------------- 180 | 181 | Create table If Not Exists Person (id int, email varchar(255)) 182 | Truncate table Person 183 | insert into Person (id, email) values ('1', 'a@b.com') 184 | insert into Person (id, email) values ('2', 'c@d.com') 185 | insert into Person (id, email) values ('3', 'a@b.com') 186 | 187 | 188 | --- 189 | 190 | # 4. Delete Duplicate Emails 191 | 192 | **Table: Person** 193 | ``` 194 | | Column Name | Type | 195 | +-------------+---------+ 196 | | id | int | 197 | | email | varchar | 198 | ``` 199 | 200 | **Id is the primary key (column with unique values) for this table.** 201 | **Each row of this table contains an email. The emails will not contain uppercase letters.** 202 | 203 | 204 | **Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id.** 205 | 206 | **Please note that you are supposed to write a DELETE statement and not a SELECT one.** 207 | 208 | 209 | 210 | After running your script, the answer shown is the Person table. 211 | The driver will first compile and run your piece of code and then show the Person table. 212 | The final order of the Person table does not matter. 213 | 214 | The result format is in the following example. 215 | --- 216 | 217 | **Input:** 218 | **Person table:** 219 | ``` 220 | | id | email | 221 | +----+------------------+ 222 | | 1 | john@example.com | 223 | | 2 | bob@example.com | 224 | | 3 | john@example.com | 225 | ``` 226 | 227 | **Output:** 228 | 229 | ``` 230 | | id | email | 231 | +----+------------------+ 232 | | 1 | john@example.com | 233 | | 2 | bob@example.com | 234 | ``` 235 | # Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1. 236 | 237 | 238 | 239 | --- 240 | 241 | # 5. Find those parents who has only girls. 242 | **Return Parent Name only.** 243 | ```sql 244 | CREATE TABLE boys ( 245 | Id INT PRIMARY KEY, 246 | name VARCHAR(100), 247 | ParentName VARCHAR(100) 248 | ); 249 | 250 | CREATE TABLE girls ( 251 | Id INT PRIMARY KEY, 252 | name VARCHAR(100), 253 | ParentName VARCHAR(100) 254 | ); 255 | 256 | INSERT INTO boys (Id, name, ParentName) 257 | VALUES 258 | (1, 'John', 'Michael'), 259 | (2, 'David', 'James'), 260 | (3, 'Alex', 'Robert'), 261 | (4, 'Luke', 'Michael'), 262 | (5, 'Ethan', 'David'), 263 | (6, 'Mason', 'George'); 264 | 265 | 266 | INSERT INTO girls (Id, name, ParentName) 267 | VALUES 268 | (1, 'Emma', 'Mike'), 269 | (2, 'Olivia', 'James'), 270 | (3, 'Ava', 'Robert'), 271 | (4, 'Sophia', 'Mike'), 272 | (5, 'Mia', 'John'), 273 | (6, 'Isabella', 'Emily'), 274 | (7, 'Charlotte', 'George'); 275 | ``` 276 | 277 | --- 278 | 279 | # 6. Total over 50 and least 280 | **Find total Sales amount for the orders which weights more than 50 for each customer along with their least weight.(from TSQL2012 database, Sales.Orders Table)** 281 | 282 | **You can find tsql2012 database in here: https://gist.github.com/alejotima/cac70484db23834591b142ad07e79e67** 283 | 284 | --- 285 | 286 | # 7. Carts 287 | 288 | **You have the tables below, write a query to get the expected output** 289 | 290 | ```sql 291 | DROP TABLE IF EXISTS Cart1; 292 | DROP TABLE IF EXISTS Cart2; 293 | GO 294 | 295 | CREATE TABLE Cart1 296 | ( 297 | Item VARCHAR(100) PRIMARY KEY 298 | ); 299 | GO 300 | 301 | CREATE TABLE Cart2 302 | ( 303 | Item VARCHAR(100) PRIMARY KEY 304 | ); 305 | GO 306 | 307 | INSERT INTO Cart1 (Item) VALUES 308 | ('Sugar'),('Bread'),('Juice'),('Soda'),('Flour'); 309 | GO 310 | 311 | INSERT INTO Cart2 (Item) VALUES 312 | ('Sugar'),('Bread'),('Butter'),('Cheese'),('Fruit'); 313 | GO 314 | ``` 315 | 316 | 317 | **Expected Output.** 318 | 319 | ``` 320 | | Item Cart 1 | Item Cart 2 | 321 | |-------------|-------------| 322 | | Sugar | Sugar | 323 | | Bread | Bread | 324 | | Juice | | 325 | | Soda | | 326 | | Flour | | 327 | | | Butter | 328 | | | Cheese | 329 | | | Fruit | 330 | ``` 331 | 332 | 333 | 334 | 335 | --- 336 | # 8. Customers Who Never Order 337 | --- 338 | 339 | **Table: Customers** 340 | ``` 341 | | Column Name | Type | 342 | +-------------+---------+ 343 | | id | int | 344 | | name | varchar | 345 | ``` 346 | 347 | 348 | **Id is the primary key (column with unique values) for this table.** 349 | **Each row of this table indicates the ID and name of a customer.** 350 | 351 | 352 | **Table: Orders** 353 | ``` 354 | | Column Name | Type | 355 | +-------------+------+ 356 | | id | int | 357 | | customerId | int | 358 | ``` 359 | 360 | **Id is the primary key (column with unique values) for this table.** 361 | **customerId is a foreign key (reference columns) of the ID from the Customers table.** 362 | **Each row of this table indicates the ID of an order and the ID of the customer who ordered it.** 363 | 364 | --- 365 | 366 | **Write a solution to find all customers who never order anything.** 367 | 368 | **Return the result table in any order.** 369 | 370 | **The result format is in the following example.** 371 | 372 | --- 373 | 374 | **Input:** 375 | **Customers table:** 376 | ``` 377 | | id | name | 378 | +----+-------+ 379 | | 1 | Joe | 380 | | 2 | Henry | 381 | | 3 | Sam | 382 | | 4 | Max | 383 | ``` 384 | 385 | **Orders table:** 386 | ``` 387 | | id | customerId | 388 | +----+------------+ 389 | | 1 | 3 | 390 | | 2 | 1 | 391 | ``` 392 | 393 | **Output:** 394 | ``` 395 | | Customers | 396 | +-----------+ 397 | | Henry | 398 | | Max | 399 | ``` 400 | 401 | ```sql 402 | Create table Customers (id int, name varchar(255)) 403 | Create table Orders (id int, customerId int) 404 | Truncate table Customers 405 | insert into Customers (id, name) values ('1', 'Joe') 406 | insert into Customers (id, name) values ('2', 'Henry') 407 | insert into Customers (id, name) values ('3', 'Sam') 408 | insert into Customers (id, name) values ('4', 'Max') 409 | Truncate table Orders 410 | insert into Orders (id, customerId) values ('1', '3') 411 | insert into Orders (id, customerId) values ('2', '1') 412 | ``` 413 | 414 | --- 415 | 416 | # 9. Students and Examinations 417 | --- 418 | 419 | **Table: Students** 420 | 421 | ``` 422 | | Column Name | Type | 423 | +---------------+---------+ 424 | | student_id | int | 425 | | student_name | varchar | 426 | ``` 427 | 428 | **student_id is the primary key (column with unique values) for this table.** 429 | **Each row of this table contains the ID and the name of one student in the school.** 430 | 431 | 432 | **Table: Subjects** 433 | 434 | ``` 435 | | Column Name | Type | 436 | +--------------+---------+ 437 | | subject_name | varchar | 438 | ``` 439 | 440 | **subject_name is the primary key (column with unique values) for this table.** 441 | **Each row of this table contains the name of one subject in the school.** 442 | 443 | **Table: Examinations** 444 | 445 | ``` 446 | | Column Name | Type | 447 | +--------------+---------+ 448 | | student_id | int | 449 | | subject_name | varchar | 450 | ``` 451 | 452 | 453 | **There is no primary key (column with unique values) for this table. It may contain duplicates.** 454 | **Each student from the Students table takes every course from the Subjects table.** 455 | **Each row of this table indicates that a student with ID student_id attended the exam of subject_name.** 456 | 457 | --- 458 | 459 | 460 | **Write a solution to find the number of times each student attended each exam.** 461 | 462 | **Return the result table ordered by student_id and subject_name.** 463 | 464 | **The result format is in the following example.** 465 | 466 | --- 467 | 468 | **Input: Students table:** 469 | ``` 470 | | student_id | student_name | 471 | +------------+--------------+ 472 | | 1 | Alice | 473 | | 2 | Bob | 474 | | 13 | John | 475 | | 6 | Alex | 476 | ``` 477 | 478 | 479 | **Subjects table:** 480 | ``` 481 | | subject_name | 482 | +--------------+ 483 | | Math | 484 | | Physics | 485 | | Programming | 486 | ``` 487 | 488 | **Examinations table:** 489 | ``` 490 | | student_id | subject_name | 491 | +------------+--------------+ 492 | | 1 | Math | 493 | | 1 | Physics | 494 | | 1 | Programming | 495 | | 2 | Programming | 496 | | 1 | Physics | 497 | | 1 | Math | 498 | | 13 | Math | 499 | | 13 | Programming | 500 | | 13 | Physics | 501 | | 2 | Math | 502 | | 1 | Math | 503 | ``` 504 | 505 | 506 | **Output:** 507 | ``` 508 | | student_id | student_name | subject_name | attended_exams | 509 | +------------+--------------+--------------+----------------+ 510 | | 1 | Alice | Math | 3 | 511 | | 1 | Alice | Physics | 2 | 512 | | 1 | Alice | Programming | 1 | 513 | | 2 | Bob | Math | 1 | 514 | | 2 | Bob | Physics | 0 | 515 | | 2 | Bob | Programming | 1 | 516 | | 6 | Alex | Math | 0 | 517 | | 6 | Alex | Physics | 0 | 518 | | 6 | Alex | Programming | 0 | 519 | | 13 | John | Math | 1 | 520 | | 13 | John | Physics | 1 | 521 | | 13 | John | Programming | 1 | 522 | ``` 523 | 524 | 525 | 526 | 527 | # Explanation: 528 | **The result table should contain all students and all subjects.** 529 | **Alice attended the Math exam 3 times, the Physics exam 2 times, and the Programming exam 1 time.** 530 | **Bob attended the Math exam 1 time, the Programming exam 1 time, and did not attend the Physics exam.** 531 | **Alex did not attend any exams.** 532 | **John attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.** 533 | 534 | --- 535 | ```sql 536 | Create table Students (student_id int, student_name varchar(20)) 537 | Create table Subjects (subject_name varchar(20)) 538 | Create table Examinations (student_id int, subject_name varchar(20)) 539 | Truncate table Students 540 | insert into Students (student_id, student_name) values ('1', 'Alice') 541 | insert into Students (student_id, student_name) values ('2', 'Bob') 542 | insert into Students (student_id, student_name) values ('13', 'John') 543 | insert into Students (student_id, student_name) values ('6', 'Alex') 544 | Truncate table Subjects 545 | insert into Subjects (subject_name) values ('Math') 546 | insert into Subjects (subject_name) values ('Physics') 547 | insert into Subjects (subject_name) values ('Programming') 548 | Truncate table Examinations 549 | insert into Examinations (student_id, subject_name) values ('1', 'Math') 550 | insert into Examinations (student_id, subject_name) values ('1', 'Physics') 551 | insert into Examinations (student_id, subject_name) values ('1', 'Programming') 552 | insert into Examinations (student_id, subject_name) values ('2', 'Programming') 553 | insert into Examinations (student_id, subject_name) values ('1', 'Physics') 554 | insert into Examinations (student_id, subject_name) values ('1', 'Math') 555 | insert into Examinations (student_id, subject_name) values ('13', 'Math') 556 | insert into Examinations (student_id, subject_name) values ('13', 'Programming') 557 | insert into Examinations (student_id, subject_name) values ('13', 'Physics') 558 | insert into Examinations (student_id, subject_name) values ('2', 'Math') 559 | insert into Examinations (student_id, subject_name) values ('1', 'Math') 560 | ``` 561 | -------------------------------------------------------------------------------- /lesson-8/homework/homework.md: -------------------------------------------------------------------------------- 1 | # lesson-8 Practice 2 | 3 | 4 | > **Notes before doing the tasks:** 5 | > - Tasks should be solved using **SQL Server**. 6 | > - Case insensitivity applies. 7 | > - Alias names do not affect the score. 8 | > - Scoring is based on the **correct output**. 9 | > - One correct solution is sufficient. 10 | 11 | 12 | ## Easy-Level Tasks 13 | 1. Using Products table, find the total number of products available in each category. 14 | 2. Using Products table, get the average price of products in the 'Electronics' category. 15 | 3. Using Customers table, list all customers from cities that start with 'L'. 16 | 4. Using Products table, get all product names that end with 'er'. 17 | 5. Using Customers table, list all customers from countries ending in 'A'. 18 | 6. Using Products table, show the highest price among all products. 19 | 7. Using Products table, label stock as 'Low Stock' if quantity < 30, else 'Sufficient'. 20 | 8. Using Customers table, find the total number of customers in each country. 21 | 9. Using Orders table, find the minimum and maximum quantity ordered. 22 | 23 | --- 24 | 25 | ## Medium-Level Tasks 26 | 10. Using Orders and Invoices tables, list customer IDs who placed orders in 2023 January to find those who did not have invoices. 27 | 11. Using Products and Products_Discounted table, Combine all product names from Products and Products_Discounted including duplicates. 28 | 12. Using Products and Products_Discounted table, Combine all product names from Products and Products_Discounted without duplicates. 29 | 13. Using Orders table, find the average order amount by year. 30 | 14. Using Products table, group products based on price: 'Low' (<100), 'Mid' (100-500), 'High' (>500). Return productname and pricegroup. 31 | 15. Using City_Population table, use Pivot to show values of Year column in seperate columns ([2012], [2013]) and copy results to a new Population_Each_Year table. 32 | 16. Using Sales table, find total sales per product Id. 33 | 17. Using Products table, use wildcard to find products that contain 'oo' in the name. Return productname. 34 | 18. Using City_Population table, use Pivot to show values of City column in seperate columns (Bektemir, Chilonzor, Yakkasaroy) and copy results to a new Population_Each_City table. 35 | --- 36 | ## Hard-Level Tasks 37 | 19. Using Invoices table, show top 3 customers with the highest total invoice amount. Return CustomerID and Totalspent. 38 | 20. Transform Population_Each_Year table to its original format (City_Population). 39 | 21. Using Products and Sales tables, list product names and the number of times each has been sold. (Research for Joins) 40 | 22. Transform Population_Each_City table to its original format (City_Population). 41 | 42 | --- 43 | 44 | **Necessary tables:** 45 | ```sql 46 | CREATE TABLE Products ( 47 | ProductID INT PRIMARY KEY, 48 | ProductName VARCHAR(100), 49 | Price DECIMAL(10, 2), 50 | Category VARCHAR(50), 51 | StockQuantity INT 52 | ); 53 | 54 | INSERT INTO Products VALUES 55 | (1, 'Laptop', 1200.00, 'Electronics', 30), 56 | (2, 'Smartphone', 800.00, 'Electronics', 50), 57 | (3, 'Tablet', 400.00, 'Electronics', 40), 58 | (4, 'Monitor', 250.00, 'Electronics', 60), 59 | (5, 'Keyboard', 50.00, 'Accessories', 100), 60 | (6, 'Mouse', 30.00, 'Accessories', 120), 61 | (7, 'Chair', 150.00, 'Furniture', 80), 62 | (8, 'Desk', 200.00, 'Furniture', 75), 63 | (9, 'Pen', 5.00, 'Stationery', 300), 64 | (10, 'Notebook', 10.00, 'Stationery', 500), 65 | (11, 'Printer', 180.00, 'Electronics', 25), 66 | (12, 'Camera', 500.00, 'Electronics', 40), 67 | (13, 'Flashlight', 25.00, 'Tools', 200), 68 | (14, 'Shirt', 30.00, 'Clothing', 150), 69 | (15, 'Jeans', 45.00, 'Clothing', 120), 70 | (16, 'Jacket', 80.00, 'Clothing', 70), 71 | (17, 'Shoes', 60.00, 'Clothing', 100), 72 | (18, 'Hat', 20.00, 'Accessories', 50), 73 | (19, 'Socks', 10.00, 'Clothing', 200), 74 | (20, 'T-Shirt', 25.00, 'Clothing', 150), 75 | (21, 'Lamp', 60.00, 'Furniture', 40), 76 | (22, 'Coffee Table', 100.00, 'Furniture', 35), 77 | (23, 'Book', 15.00, 'Stationery', 250), 78 | (24, 'Rug', 90.00, 'Furniture', 60), 79 | (25, 'Cup', 5.00, 'Accessories', 500), 80 | (26, 'Bag', 25.00, 'Accessories', 300), 81 | (27, 'Couch', 450.00, 'Furniture', 15), 82 | (28, 'Fridge', 600.00, 'Electronics', 20), 83 | (29, 'Stove', 500.00, 'Electronics', 15), 84 | (30, 'Microwave', 120.00, 'Electronics', 25), 85 | (31, 'Air Conditioner', 350.00, 'Electronics', 10), 86 | (32, 'Washing Machine', 450.00, 'Electronics', 15), 87 | (33, 'Dryer', 400.00, 'Electronics', 10), 88 | (34, 'Hair Dryer', 30.00, 'Accessories', 100), 89 | (35, 'Iron', 40.00, 'Electronics', 50), 90 | (36, 'Coffee Maker', 50.00, 'Electronics', 60), 91 | (37, 'Blender', 35.00, 'Electronics', 40), 92 | (38, 'Juicer', 55.00, 'Electronics', 30), 93 | (39, 'Toaster', 40.00, 'Electronics', 70), 94 | (40, 'Dishwasher', 500.00, 'Electronics', 20); 95 | 96 | CREATE TABLE Customers ( 97 | CustomerID INT PRIMARY KEY, 98 | FirstName VARCHAR(100), 99 | LastName VARCHAR(100), 100 | Email VARCHAR(100), 101 | Phone VARCHAR(50), 102 | Address VARCHAR(255), 103 | City VARCHAR(100), 104 | State VARCHAR(100), 105 | PostalCode VARCHAR(20), 106 | Country VARCHAR(100) 107 | ); 108 | 109 | --2. Insert 40 Rows into Customers Table 110 | INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Phone, Address, City, State, PostalCode, Country) VALUES 111 | (1, 'John', 'Doe', 'johndoe@gmail.com', '555-1234', '123 Elm St', 'New York', 'NY', '10001', 'USA'), 112 | (2, 'Jane', 'Smith', 'janesmith@yahoo.com', '555-2345', '456 Oak St', 'Los Angeles', 'CA', '90001', 'USA'), 113 | (3, 'Alice', 'Johnson', 'alicej@outlook.com', '555-3456', '789 Pine St', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 114 | (4, 'Bob', 'Brown', 'bobbrown@hotmail.com', '555-4567', '101 Maple St', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 115 | (5, 'Charlie', 'Davis', 'charliedavis@gmail.com', '555-5678', '202 Birch St', 'Sydney', 'NSW', '2000', 'Australia'), 116 | (6, 'David', 'Martinez', 'davidm@live.com', '555-6789', '303 Cedar St', 'Melbourne', 'VIC', '3000', 'Australia'), 117 | (7, 'Emily', 'Garcia', 'emilyg@yahoo.com', '555-7890', '404 Redwood St', 'London', 'England', 'SW1A 1AA', 'UK'), 118 | (8, 'Frank', 'Hernandez', 'frankh@outlook.com', '555-8901', '505 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 119 | (9, 'Grace', 'Lopez', 'gracel@gmail.com', '555-9012', '606 Aspen St', 'Birmingham', 'England', 'B1 1AA', 'UK'), 120 | (10, 'Helen', 'Gonzalez', 'heleng@yahoo.com', '555-0123', '707 Fir St', 'Berlin', 'BE', '10117', 'Germany'), 121 | (11, 'Irene', 'Perez', 'irenep@hotmail.com', '555-1234', '808 Maple Ave', 'Munich', 'BY', '80331', 'Germany'), 122 | (12, 'Jack', 'Wilson', 'jackw@live.com', '555-2345', '909 Oak Ave', 'Hamburg', 'HH', '20095', 'Germany'), 123 | (13, 'Kim', 'Anderson', 'kima@gmail.com', '555-3456', '111 Pine Ave', 'Paris', '�le-de-France', '75001', 'France'), 124 | (14, 'Liam', 'Thomas', 'liamt@yahoo.com', '555-4567', '222 Cedar Ave', 'Lyon', 'Auvergne-Rh�ne-Alpes', '69001', 'France'), 125 | (15, 'Megan', 'Taylor', 'megant@outlook.com', '555-5678', '333 Redwood Ave', 'Marseille', 'Provence-Alpes-C�te Azur', '13001', 'France'), 126 | (16, 'Nathan', 'Moore', 'nathanm@hotmail.com', '555-6789', '444 Willow Ave', 'Tokyo', 'TK', '100-0001', 'Japan'), 127 | (17, 'Olivia', 'Jackson', 'oliviaj@gmail.com', '555-7890', '555 Birch Ave', 'Osaka', 'OS', '530-0001', 'Japan'), 128 | (18, 'Paul', 'White', 'paulw@yahoo.com', '555-8901', '666 Maple Blvd', 'Kyoto', 'KY', '600-8001', 'Japan'), 129 | (19, 'Quincy', 'Lee', 'quincyl@outlook.com', '555-9012', '777 Oak Blvd', 'Seoul', 'SO', '04547', 'South Korea'), 130 | (20, 'Rachel', 'Harris', 'rachelh@hotmail.com', '555-0123', '888 Pine Blvd', 'Busan', 'BU', '48058', 'South Korea'), 131 | (21, 'Sam', 'Clark', 'samc@gmail.com', '555-1234', '999 Cedar Blvd', 'Incheon', 'IC', '22382', 'South Korea'), 132 | (22, 'Tina', 'Allen', 'tinaallen@yahoo.com', '555-2345', '1010 Redwood Blvd', 'Mexico City', 'CMX', '01000', 'Mexico'), 133 | (23, 'Ursula', 'Scott', 'ursulas@outlook.com', '555-3456', '1111 Willow Blvd', 'Guadalajara', 'JAL', '44100', 'Mexico'), 134 | (24, 'Victor', 'Adams', 'victora@hotmail.com', '555-4567', '1212 Birch Blvd', 'Monterrey', 'NLE', '64000', 'Mexico'), 135 | (25, 'Walter', 'Baker', 'walterb@live.com', '555-5678', '1313 Oak Blvd', 'New York', 'NY', '10001', 'USA'), 136 | (26, 'Xander', 'Nelson', 'xandern@gmail.com', '555-6789', '1414 Cedar Blvd', 'Los Angeles', 'CA', '90001', 'USA'), 137 | (27, 'Yvonne', 'Carter', 'yvonnec@yahoo.com', '555-7890', '1515 Maple Dr', 'Chicago', 'IL', '60601', 'USA'), 138 | (28, 'Zane', 'Mitchell', 'zanem@outlook.com', '555-8901', '1616 Redwood Dr', 'Houston', 'TX', '77001', 'USA'), 139 | (29, 'Anna', 'Roberts', 'annar@hotmail.com', '555-9012', '1717 Willow Dr', 'Sydney', 'NSW', '2000', 'Australia'), 140 | (30, 'Ben', 'King', 'benk@live.com', '555-0123', '1818 Birch Dr', 'Melbourne', 'VIC', '3000', 'Australia'), 141 | (31, 'Chloe', 'Green', 'chloeg@gmail.com', '555-1234', '1919 Oak Dr', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 142 | (32, 'Daniel', 'Evans', 'daniele@yahoo.com', '555-2345', '2020 Cedar Dr', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 143 | (33, 'Ella', 'Collins', 'ellac@outlook.com', '555-3456', '2121 Redwood Dr', 'London', 'England', 'SW1A 1AA', 'UK'), 144 | (34, 'Finn', 'Morris', 'finnm@hotmail.com', '555-4567', '2222 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 145 | (35, 'Grace', 'Lee', 'gracel@live.com', '555-5678', '2323 Birch St', 'Berlin', 'BE', '10117', 'Germany'), 146 | (36, 'Holly', 'Martinez', 'hollym@gmail.com', '555-6789', '2424 Oak St', 'Munich', 'BY', '80331', 'Germany'), 147 | (37, 'Ian', 'Robinson', 'ianr@yahoo.com', '555-7890', '2525 Cedar St', 'Warsaw', 'WA', '00-001', 'Poland'), 148 | (38, 'Jasmine', 'Walker', 'jasminew@outlook.com', '555-8901', '2626 Redwood St', 'Lisbon', 'LI', '1100-148', 'Portugal'), 149 | (39, 'Kyle', 'Young', 'kyley@hotmail.com', '555-9012', '2727 Willow St', 'Pittsburgh', 'PA', '15201','USA'), 150 | (40, 'Liam', 'Harris', 'liamh@live.com', '555-0123', '2828 Birch St', 'Richmond', 'VA', '23220','USA'); 151 | 152 | 153 | 154 | CREATE TABLE Products_Discounted ( 155 | ProductID INT PRIMARY KEY, 156 | ProductName VARCHAR(100), 157 | Price DECIMAL(10, 2), 158 | Category VARCHAR(50), 159 | StockQuantity INT 160 | ); 161 | 162 | INSERT INTO Products_Discounted VALUES 163 | (1, 'Gaming Laptop', 950.00, 'Electronics', 25), 164 | (2, 'Smartphone', 750.00, 'Electronics', 45), 165 | (3, 'Convertible Tablet', 350.00, 'Electronics', 35), 166 | (4, 'Ultra-Wide Monitor', 220.00, 'Electronics', 55), 167 | (5, 'Mechanical Keyboard', 45.00, 'Accessories', 90), 168 | (6, 'Wireless Mouse', 25.00, 'Accessories', 110), 169 | (7, 'Chair', 130.00, 'Furniture', 75), 170 | (8, 'Standing Desk', 190.00, 'Furniture', 70), 171 | (9, 'Luxury Pen', 4.50, 'Stationery', 280), 172 | (10, 'Leather Notebook', 9.00, 'Stationery', 480), 173 | (11, 'Laser Printer', 160.00, 'Electronics', 20), 174 | (12, 'DSLR Camera', 480.00, 'Electronics', 35), 175 | (13, 'LED Flashlight', 20.00, 'Tools', 190), 176 | (14, 'Designer Shirt', 28.00, 'Clothing', 140), 177 | (15, 'Jeans', 40.00, 'Clothing', 110), 178 | (16, 'Winter Jacket', 70.00, 'Clothing', 60), 179 | (17, 'Running Shoes', 55.00, 'Clothing', 90), 180 | (18, 'Wool Hat', 18.00, 'Accessories', 45), 181 | (19, 'Thermal Socks', 9.00, 'Clothing', 180), 182 | (20, 'T-Shirt', 22.00, 'Clothing', 140), 183 | (21, 'Desk Lamp', 55.00, 'Furniture', 35), 184 | (22, 'Marble Coffee Table', 95.00, 'Furniture', 30), 185 | (23, 'Hardcover Book', 13.00, 'Stationery', 230), 186 | (24, 'Persian Rug', 85.00, 'Furniture', 50), 187 | (25, 'Glass Cup', 4.50, 'Accessories', 470), 188 | (26, 'Leather Bag', 22.00, 'Accessories', 270), 189 | (27, 'Recliner Couch', 420.00, 'Furniture', 10), 190 | (28, 'Smart Fridge', 570.00, 'Electronics', 15), 191 | (29, 'Gas Stove', 460.00, 'Electronics', 12), 192 | (30, 'Compact Microwave', 100.00, 'Electronics', 20), 193 | (31, 'Split Air Conditioner', 320.00, 'Electronics', 8), 194 | (32, 'Front-Load Washing Machine', 410.00, 'Electronics', 12), 195 | (33, 'High-Efficiency Dryer', 370.00, 'Electronics', 8), 196 | (34, 'Ionic Hair Dryer', 27.00, 'Accessories', 90), 197 | (35, 'Steam Iron', 38.00, 'Electronics', 45), 198 | (36, 'Espresso Maker', 45.00, 'Electronics', 55), 199 | (37, 'Portable Blender', 32.00, 'Electronics', 35), 200 | (38, 'Cold Press Juicer', 50.00, 'Electronics', 28), 201 | (39, 'Smart Toaster', 36.00, 'Electronics', 65), 202 | (40, 'Compact Dishwasher', 470.00, 'Electronics', 18); 203 | 204 | CREATE TABLE Sales ( 205 | SaleID INT PRIMARY KEY, 206 | ProductID INT, 207 | CustomerID INT, 208 | SaleDate DATE, 209 | SaleAmount DECIMAL(10, 2) 210 | ); 211 | 212 | INSERT INTO Sales (SaleID, ProductID, CustomerID, SaleDate, SaleAmount) VALUES 213 | (1, 1, 1, '2023-01-01', 150.00), 214 | (2, 2, 2, '2023-01-02', 200.00), 215 | (3, 3, 3, '2023-01-03', 250.00), 216 | (4, 4, 4, '2023-01-04', 300.00), 217 | (5, 5, 5, '2023-01-05', 350.00), 218 | (6, 6, 6, '2023-01-06', 400.00), 219 | (7, 7, 7, '2023-01-07', 450.00), 220 | (8, 8, 8, '2023-01-08', 500.00), 221 | (9, 9, 9, '2023-01-09', 550.00), 222 | (10, 10, 10, '2023-01-10', 600.00), 223 | (11, 1, 1, '2023-01-11', 150.00), 224 | (12, 2, 2, '2023-01-12', 200.00), 225 | (13, 3, 3, '2023-01-13', 250.00), 226 | (14, 4, 4, '2023-01-14', 300.00), 227 | (15, 5, 5, '2023-01-15', 350.00), 228 | (16, 6, 6, '2023-01-16', 400.00), 229 | (17, 7, 7, '2023-01-17', 450.00), 230 | (18, 8, 8, '2023-01-18', 500.00), 231 | (19, 9, 9, '2023-01-19', 550.00), 232 | (20, 10, 10, '2023-01-20', 600.00), 233 | (21, 1, 2, '2023-01-21', 150.00), 234 | (22, 2, 3, '2023-01-22', 200.00), 235 | (23, 3, 4, '2023-01-23', 250.00), 236 | (24, 4, 5, '2023-01-24', 300.00), 237 | (25, 5, 6, '2023-01-25', 350.00), 238 | (26, 6, 7, '2023-01-26', 400.00), 239 | (27, 7, 8, '2023-01-27', 450.00), 240 | (28, 8, 9, '2023-01-28', 500.00), 241 | (29, 9, 10, '2023-01-29', 550.00), 242 | (30, 10, 1, '2023-01-30', 600.00), 243 | (31, 1, 2, '2023-02-01', 150.00), 244 | (32, 2, 3, '2023-02-02', 200.00), 245 | (33, 3, 4, '2023-02-03', 250.00), 246 | (34, 4, 5, '2023-02-04', 300.00), 247 | (35, 5, 6, '2023-02-05', 350.00), 248 | (36, 6, 7, '2023-02-06', 400.00), 249 | (37, 7, 8, '2023-02-07', 450.00), 250 | (38, 8, 9, '2023-02-08', 500.00), 251 | (39, 9, 10, '2023-02-09', 550.00), 252 | (40, 10, 1, '2023-02-10', 600.00); 253 | 254 | CREATE TABLE Orders ( 255 | OrderID INT PRIMARY KEY, 256 | CustomerID INT, 257 | ProductID INT, 258 | OrderDate DATE, 259 | Quantity INT, 260 | TotalAmount DECIMAL(10, 2), 261 | FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID), 262 | FOREIGN KEY (ProductID) REFERENCES Products(ProductID) 263 | ); 264 | 265 | -- Insert 40 rows into Orders with random dates and years 266 | INSERT INTO Orders VALUES 267 | (1, 1, 2, '2023-05-14', 1, 800.00), 268 | (2, 2, 3, '2024-09-07', 2, 800.00), 269 | (3, 3, 4, '2022-11-22', 1, 250.00), 270 | (4, 4, 5, '2021-03-30', 3, 150.00), 271 | (5, 5, 6, '2025-07-19', 1, 30.00), 272 | (6, 6, 7, '2022-08-25', 2, 300.00), 273 | (7, 7, 8, '2024-06-10', 1, 200.00), 274 | (8, 8, 9, '2021-12-04', 4, 40.00), 275 | (9, 9, 10, '2023-02-18', 1, 10.00), 276 | (10, 10, 11, '2025-09-27', 2, 360.00), 277 | (11, 11, 12, '2023-10-11', 1, 500.00), 278 | (12, 12, 13, '2024-04-03', 1, 25.00), 279 | (13, 13, 14, '2022-07-29', 2, 60.00), 280 | (14, 14, 15, '2021-01-22', 3, 135.00), 281 | (15, 15, 16, '2025-11-15', 1, 80.00), 282 | (16, 16, 17, '2022-10-08', 1, 60.00), 283 | (17, 17, 18, '2023-06-21', 2, 40.00), 284 | (18, 18, 19, '2021-09-13', 5, 50.00), 285 | (19, 19, 20, '2025-03-05', 2, 50.00), 286 | (20, 20, 21, '2024-08-14', 1, 60.00), 287 | (21, 21, 22, '2022-12-01', 1, 100.00), 288 | (22, 22, 23, '2023-09-09', 1, 15.00), 289 | (23, 23, 24, '2021-07-18', 2, 180.00), 290 | (24, 24, 25, '2025-06-23', 3, 15.00), 291 | (25, 25, 26, '2023-03-12', 4, 100.00), 292 | (26, 26, 27, '2022-04-07', 1, 450.00), 293 | (27, 27, 28, '2024-11-30', 1, 600.00), 294 | (28, 28, 29, '2021-02-25', 1, 500.00), 295 | (29, 29, 30, '2025-05-28', 2, 240.00), 296 | (30, 30, 31, '2023-08-20', 1, 350.00), 297 | (31, 31, 32, '2022-01-17', 1, 450.00), 298 | (32, 32, 33, '2025-09-10', 1, 40.00), 299 | (33, 33, 34, '2021-04-04', 2, 100.00), 300 | (34, 34, 35, '2024-07-15', 3, 120.00), 301 | (35, 35, 36, '2022-10-31', 1, 60.00), 302 | (36, 36, 37, '2023-12-22', 1, 35.00), 303 | (37, 37, 38, '2021-06-06', 2, 110.00), 304 | (38, 38, 39, '2025-02-01', 1, 40.00), 305 | (39, 39, 40, '2023-11-26', 3, 120.00), 306 | (40, 40, 1, '2024-03-09', 1, 1200.00); 307 | 308 | 309 | CREATE TABLE Invoices ( 310 | InvoiceID INT PRIMARY KEY, 311 | CustomerID INT, 312 | InvoiceDate DATE, 313 | TotalAmount DECIMAL(10, 2) 314 | ); 315 | 316 | INSERT INTO Invoices (InvoiceID, CustomerID, InvoiceDate, TotalAmount) VALUES 317 | (1, 1, '2023-01-05', 150.00), 318 | (2, 2, '2023-01-07', 200.00), 319 | (3, 3, '2023-01-10', 250.00), 320 | (4, 4, '2023-01-12', 300.00), 321 | (5, 5, '2023-01-15', 350.00), 322 | (6, 6, '2023-01-18', 400.00), 323 | (7, 7, '2023-01-20', 450.00), 324 | (8, 8, '2023-01-23', 500.00), 325 | (9, 9, '2023-01-25', 550.00), 326 | (10, 10, '2023-01-28', 600.00), 327 | (11, 11, '2023-02-02', 150.00), 328 | (12, 12, '2023-02-04', 200.00), 329 | (13, 13, '2023-02-07', 250.00), 330 | (14, 14, '2023-02-09', 300.00), 331 | (15, 15, '2023-02-11', 350.00), 332 | (16, 16, '2023-02-13', 400.00), 333 | (17, 17, '2023-02-15', 450.00), 334 | (18, 18, '2023-02-17', 500.00), 335 | (19, 19, '2023-02-19', 550.00), 336 | (20, 20, '2023-02-21', 600.00), 337 | (21, 21, '2023-02-24', 150.00), 338 | (22, 22, '2023-02-26', 200.00), 339 | (23, 23, '2023-02-28', 250.00), 340 | (24, 24, '2023-03-02', 300.00), 341 | (25, 25, '2023-03-04', 350.00), 342 | (26, 26, '2023-03-06', 400.00), 343 | (27, 27, '2023-03-08', 450.00), 344 | (28, 28, '2023-03-10', 500.00), 345 | (29, 29, '2023-03-12', 550.00), 346 | (30, 30, '2023-03-14', 600.00), 347 | (31, 31, '2023-03-17', 150.00), 348 | (32, 32, '2023-03-19', 200.00), 349 | (33, 33, '2023-03-21', 250.00), 350 | (34, 34, '2023-03-23', 300.00), 351 | (35, 35, '2023-03-25', 350.00), 352 | (36, 36, '2023-03-27', 400.00), 353 | (37, 37, '2023-03-29', 450.00), 354 | (38, 38, '2023-03-31', 500.00), 355 | (39, 39, '2023-04-02', 550.00), 356 | (40, 40, '2023-04-04', 600.00); 357 | 358 | create table city_population ( district_id int, district_name varchar(30),population decimal(10,2),year varchar(20)) 359 | 360 | insert into city_population values 361 | (1,'Chilonzor',2500,2012), 362 | (2,'Yakkasaroy',1500,2012), 363 | (3,'Mirobod',3000,2012), 364 | (4,'Yashnobod',1000,2012), 365 | (5,'Bektemir',2000,2012), 366 | (1,'Chilonzor',2800,2013), 367 | (2,'Yakkasaroy',1900,2013), 368 | (3,'Mirobod',2000,2013), 369 | (4,'Yashnobod',5000,2013), 370 | (5,'Bektemir',1500,2013) 371 | 372 | ``` 373 | -------------------------------------------------------------------------------- /lesson-7/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 7 Homework Tasks 2 | 3 | These homework tasks cover the following topics: 4 | - **Aggregate Functions** (MIN, MAX, COUNT, AVG, SUM) 5 | - **Filtering Aggregated Data with HAVING** 6 | 7 | > **Notes before doing the tasks:** 8 | > - Tasks should be solved using **SQL Server**. 9 | > - Case insensitivity applies. 10 | > - Alias names do not affect the score. 11 | > - Scoring is based on the **correct output**. 12 | > - One correct solution is sufficient. 13 | 14 | --- 15 | 16 | # 🟢 Easy-Level Tasks (10) 17 | 18 | 1. Write a query to find the minimum (MIN) price of a product in the Products table. 19 | 2. Write a query to find the maximum (MAX) Salary from the Employees table. 20 | 3. Write a query to count the number of rows in the Customers table. 21 | 4. Write a query to count the number of unique product categories from the Products table. 22 | 5. Write a query to find the total sales amount for the product with id 7 in the Sales table. 23 | 6. Write a query to calculate the average age of employees in the Employees table. 24 | 7. Write a query to count the number of employees in each department. 25 | 8. Write a query to show the minimum and maximum Price of products grouped by Category. Use products table. 26 | 9. Write a query to calculate the total sales per Customer in the Sales table. 27 | 10. Write a query to filter departments having more than 5 employees from the Employees table.(DeptID is enough, if you don't have DeptName). 28 | 29 | --- 30 | 31 | # 🟠 Medium-Level Tasks (9) 32 | 33 | 11. Write a query to calculate the total sales and average sales for each product category from the Sales table. 34 | 12. Write a query to count the number of employees from the Department HR. 35 | 13. Write a query that finds the highest and lowest Salary by department in the Employees table.(DeptID is enough, if you don't have DeptName). 36 | 14. Write a query to calculate the average salary per Department.(DeptID is enough, if you don't have DeptName). 37 | 15. Write a query to show the AVG salary and COUNT(*) of employees working in each department.(DeptID is enough, if you don't have DeptName). 38 | 16. Write a query to filter product categories with an average price greater than 400. 39 | 17. Write a query that calculates the total sales for each year in the Sales table. 40 | 18. Write a query to show the list of customers who placed at least 3 orders. 41 | 19. Write a query to filter out Departments with average salary expenses greater than 60000.(DeptID is enough, if you don't have DeptName). 42 | 43 | --- 44 | 45 | # 🔴 Hard-Level Tasks (6) 46 | 47 | 20. Write a query that shows the average price for each product category, and then filter categories with an average price greater than 150. 48 | 21. Write a query to calculate the total sales for each Customer, then filter the results to include only Customers with total sales over 1500. 49 | 22. Write a query to find the total and average salary of employees in each department, and filter the output to include only departments with an average salary greater than 65000. 50 | 23. Write a query to find total amount for the orders which weights more than $50 for each customer along with their least purchases.(least amount might be lower than 50, use tsql2012.sales.orders table,freight col, ask ur assistant to give the TSQL2012 database). 51 | 25. Write a query that calculates the total sales and counts unique products sold in each month of each year, and then filter the months with at least 2 products sold.(Orders) 52 | 26. Write a query to find the MIN and MAX order quantity per Year. From orders table. 53 | **Necessary tables:** 54 | ```sql 55 | DROP TABLE IF EXISTS Employees; 56 | 57 | CREATE TABLE Employees ( 58 | EmployeeID INT PRIMARY KEY, 59 | FirstName VARCHAR(50) NULL, 60 | LastName VARCHAR(50) NULL, 61 | DepartmentName VARCHAR(50), 62 | Salary DECIMAL(10, 2), 63 | HireDate DATE, 64 | Age INT, 65 | Email VARCHAR(100) NULL, 66 | Country VARCHAR(50) 67 | ); 68 | 69 | INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentName, Salary, HireDate, Age, Email, Country) VALUES 70 | (1, 'John', 'Doe', 'IT', 55000.00, '2020-01-01', 30, 'johndoe@example.com', 'USA'), 71 | (2, 'Jane', 'Smith', 'HR', 65000.00, '2019-03-15', 28, 'janesmith@example.com', 'USA'), 72 | (3, NULL, 'Johnson', 'Finance', 45000.00, '2021-05-10', 25, NULL, 'Canada'), 73 | (4, 'James', 'Brown', 'Marketing', 60000.00, '2018-07-22', 35, 'jamesbrown@example.com', 'UK'), 74 | (5, 'Patricia', NULL, 'HR', 70000.00, '2017-08-30', 40, NULL, 'USA'), 75 | (6, 'Michael', 'Miller', 'IT', 75000.00, '2020-12-12', 27, 'michaelm@example.com', 'Germany'), 76 | (7, 'Linda', NULL, 'Finance', 48000.00, '2016-11-02', 42, NULL, 'Canada'), 77 | (8, 'David', 'Moore', 'Marketing', 85000.00, '2021-09-01', 29, 'davidm@example.com', 'UK'), 78 | (9, 'Elizabeth', 'Taylor', 'HR', 60000.00, '2019-05-18', 31, 'elizabetht@example.com', 'USA'), 79 | (10, 'William', NULL, 'IT', 64000.00, '2020-04-10', 26, NULL, 'Germany'), 80 | (11, NULL, 'Thomas', 'Finance', 47000.00, '2017-01-25', 38, NULL, 'Canada'), 81 | (12, 'Joseph', 'Jackson', 'Marketing', 78000.00, '2016-09-30', 44, 'josephj@example.com', 'UK'), 82 | (13, 'Karen', 'White', 'HR', 59000.00, '2018-06-10', 33, 'karenw@example.com', 'USA'), 83 | (14, 'Steven', NULL, 'IT', 71000.00, '2021-07-15', 24, NULL, 'Germany'), 84 | (15, 'Nancy', 'Clark', 'Finance', 45000.00, '2020-02-20', 27, 'nancyc@example.com', 'Canada'), 85 | (16, 'George', 'Lewis', 'Marketing', 80000.00, '2019-11-10', 36, 'georgel@example.com', 'UK'), 86 | (17, 'Betty', NULL, 'HR', 55000.00, '2017-04-05', 41, NULL, 'USA'), 87 | (18, 'Samuel', 'Walker', 'IT', 72000.00, '2021-03-22', 23, 'samuelw@example.com', 'Germany'), 88 | (19, 'Helen', 'Hall', 'Finance', 49000.00, '2018-10-16', 34, 'helenh@example.com', 'Canada'), 89 | (20, NULL, 'Allen', 'Marketing', 90000.00, '2015-08-11', 50, NULL, 'UK'), 90 | (21, 'Betty', 'Young', 'HR', 53000.00, '2020-05-17', 28, 'bettyy@example.com', 'USA'), 91 | (22, 'Frank', NULL, 'IT', 67000.00, '2021-02-02', 26, 'frankk@example.com', 'Germany'), 92 | (23, 'Deborah', 'Scott', 'Finance', 47000.00, '2019-07-09', 29, NULL, 'Canada'), 93 | (24, 'Matthew', 'Green', 'Marketing', 76000.00, '2021-01-15', 30, 'matthewg@example.com', 'UK'), 94 | (25, NULL, 'Adams', 'HR', 54000.00, '2020-06-21', 27, NULL, 'USA'), 95 | (26, 'Paul', 'Nelson', 'IT', 71000.00, '2018-12-03', 37, 'pauln@example.com', 'Germany'), 96 | (27, 'Margaret', NULL, 'Finance', 46000.00, '2020-08-19', 25, 'margaretc@example.com', 'Canada'), 97 | (28, 'Anthony', 'Mitchell', 'Marketing', 82000.00, '2021-04-10', 29, NULL, 'UK'), 98 | (29, 'Lisa', 'Perez', 'HR', 60000.00, '2021-03-05', 24, 'lisap@example.com', 'USA'), 99 | (30, NULL, 'Roberts', 'IT', 69000.00, '2019-09-24', 32, NULL, 'Germany'), 100 | (31, 'Jessica', 'Gonzalez', 'Finance', 47000.00, '2017-12-13', 38, 'jessicag@example.com', 'Canada'), 101 | (32, 'Brian', NULL, 'Marketing', 85000.00, '2018-11-05', 35, NULL, 'UK'), 102 | (33, 'Dorothy', 'Evans', 'HR', 59000.00, '2019-06-11', 31, 'dorothye@example.com', 'USA'), 103 | (34, 'Matthew', 'Carter', 'IT', 70000.00, '2020-01-29', 29, 'matthewc@example.com', 'Germany'), 104 | (35, NULL, 'Martinez', 'Finance', 51000.00, '2021-06-06', 22, NULL, 'Canada'), 105 | (36, 'Daniel', 'Perez', 'Marketing', 83000.00, '2021-07-01', 30, 'danielp@example.com', 'UK'), 106 | (37, 'Catherine', 'Roberts', 'HR', 60000.00, '2020-09-19', 27, 'catheriner@example.com', 'USA'), 107 | (38, 'Ronald', NULL, 'IT', 68000.00, '2017-02-04', 39, NULL, 'Germany'), 108 | (39, 'Angela', 'Jenkins', 'Finance', 52000.00, '2018-04-23', 34, 'angelaj@example.com', 'Canada'), 109 | (40, 'Gary', 'Wright', 'Marketing', 87000.00, '2021-01-10', 29, NULL, 'UK'); 110 | 111 | DROP TABLE IF EXISTS Orders; 112 | DROP TABLE IF EXISTS Products; 113 | DROP TABLE IF EXISTS Customers; 114 | 115 | CREATE TABLE Products ( 116 | ProductID INT PRIMARY KEY, 117 | ProductName VARCHAR(100), 118 | Price DECIMAL(10, 2), 119 | Category VARCHAR(50), 120 | StockQuantity INT 121 | ); 122 | 123 | INSERT INTO Products VALUES 124 | (1, 'Laptop', 1200.00, 'Electronics', 30), 125 | (2, 'Smartphone', 800.00, 'Electronics', 50), 126 | (3, 'Tablet', 400.00, 'Electronics', 40), 127 | (4, 'Monitor', 250.00, 'Electronics', 60), 128 | (5, 'Keyboard', 50.00, 'Accessories', 100), 129 | (6, 'Mouse', 30.00, 'Accessories', 120), 130 | (7, 'Chair', 150.00, 'Furniture', 80), 131 | (8, 'Desk', 200.00, 'Furniture', 75), 132 | (9, 'Pen', 5.00, 'Stationery', 300), 133 | (10, 'Notebook', 10.00, 'Stationery', 500), 134 | (11, 'Printer', 180.00, 'Electronics', 25), 135 | (12, 'Camera', 500.00, 'Electronics', 40), 136 | (13, 'Flashlight', 25.00, 'Tools', 200), 137 | (14, 'Shirt', 30.00, 'Clothing', 150), 138 | (15, 'Jeans', 45.00, 'Clothing', 120), 139 | (16, 'Jacket', 80.00, 'Clothing', 70), 140 | (17, 'Shoes', 60.00, 'Clothing', 100), 141 | (18, 'Hat', 20.00, 'Accessories', 50), 142 | (19, 'Socks', 10.00, 'Clothing', 200), 143 | (20, 'T-Shirt', 25.00, 'Clothing', 150), 144 | (21, 'Lamp', 60.00, 'Furniture', 40), 145 | (22, 'Coffee Table', 100.00, 'Furniture', 35), 146 | (23, 'Book', 15.00, 'Stationery', 250), 147 | (24, 'Rug', 90.00, 'Furniture', 60), 148 | (25, 'Cup', 5.00, 'Accessories', 500), 149 | (26, 'Bag', 25.00, 'Accessories', 300), 150 | (27, 'Couch', 450.00, 'Furniture', 15), 151 | (28, 'Fridge', 600.00, 'Electronics', 20), 152 | (29, 'Stove', 500.00, 'Electronics', 15), 153 | (30, 'Microwave', 120.00, 'Electronics', 25), 154 | (31, 'Air Conditioner', 350.00, 'Electronics', 10), 155 | (32, 'Washing Machine', 450.00, 'Electronics', 15), 156 | (33, 'Dryer', 400.00, 'Electronics', 10), 157 | (34, 'Hair Dryer', 30.00, 'Accessories', 100), 158 | (35, 'Iron', 40.00, 'Electronics', 50), 159 | (36, 'Coffee Maker', 50.00, 'Electronics', 60), 160 | (37, 'Blender', 35.00, 'Electronics', 40), 161 | (38, 'Juicer', 55.00, 'Electronics', 30), 162 | (39, 'Toaster', 40.00, 'Electronics', 70), 163 | (40, 'Dishwasher', 500.00, 'Electronics', 20); 164 | 165 | CREATE TABLE Customers ( 166 | CustomerID INT PRIMARY KEY, 167 | FirstName VARCHAR(100), 168 | LastName VARCHAR(100), 169 | Email VARCHAR(100), 170 | Phone VARCHAR(50), 171 | Address VARCHAR(255), 172 | City VARCHAR(100), 173 | State VARCHAR(100), 174 | PostalCode VARCHAR(20), 175 | Country VARCHAR(100) 176 | ); 177 | 178 | INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Phone, Address, City, State, PostalCode, Country) VALUES 179 | (1, 'John', 'Doe', 'johndoe@gmail.com', '555-1234', '123 Elm St', 'New York', 'NY', '10001', 'USA'), 180 | (2, 'Jane', 'Smith', 'janesmith@yahoo.com', '555-2345', '456 Oak St', 'Los Angeles', 'CA', '90001', 'USA'), 181 | (3, 'Alice', 'Johnson', 'alicej@outlook.com', '555-3456', '789 Pine St', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 182 | (4, 'Bob', 'Brown', 'bobbrown@hotmail.com', '555-4567', '101 Maple St', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 183 | (5, 'Charlie', 'Davis', 'charliedavis@gmail.com', '555-5678', '202 Birch St', 'Sydney', 'NSW', '2000', 'Australia'), 184 | (6, 'David', 'Martinez', 'davidm@live.com', '555-6789', '303 Cedar St', 'Melbourne', 'VIC', '3000', 'Australia'), 185 | (7, 'Emily', 'Garcia', 'emilyg@yahoo.com', '555-7890', '404 Redwood St', 'London', 'England', 'SW1A 1AA', 'UK'), 186 | (8, 'Frank', 'Hernandez', 'frankh@outlook.com', '555-8901', '505 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 187 | (9, 'Grace', 'Lopez', 'gracel@gmail.com', '555-9012', '606 Aspen St', 'Birmingham', 'England', 'B1 1AA', 'UK'), 188 | (10, 'Helen', 'Gonzalez', 'heleng@yahoo.com', '555-0123', '707 Fir St', 'Berlin', 'BE', '10117', 'Germany'), 189 | (11, 'Irene', 'Perez', 'irenep@hotmail.com', '555-1234', '808 Maple Ave', 'Munich', 'BY', '80331', 'Germany'), 190 | (12, 'Jack', 'Wilson', 'jackw@live.com', '555-2345', '909 Oak Ave', 'Hamburg', 'HH', '20095', 'Germany'), 191 | (13, 'Kim', 'Anderson', 'kima@gmail.com', '555-3456', '111 Pine Ave', 'Paris', 'Île-de-France', '75001', 'France'), 192 | (14, 'Liam', 'Thomas', 'liamt@yahoo.com', '555-4567', '222 Cedar Ave', 'Lyon', 'Auvergne-Rhône-Alpes', '69001', 'France'), 193 | (15, 'Megan', 'Taylor', 'megant@outlook.com', '555-5678', '333 Redwood Ave', 'Marseille', 'Provence-Alpes-Côte Azur', '13001', 'France'), 194 | (16, 'Nathan', 'Moore', 'nathanm@hotmail.com', '555-6789', '444 Willow Ave', 'Tokyo', 'TK', '100-0001', 'Japan'), 195 | (17, 'Olivia', 'Jackson', 'oliviaj@gmail.com', '555-7890', '555 Birch Ave', 'Osaka', 'OS', '530-0001', 'Japan'), 196 | (18, 'Paul', 'White', 'paulw@yahoo.com', '555-8901', '666 Maple Blvd', 'Kyoto', 'KY', '600-8001', 'Japan'), 197 | (19, 'Quincy', 'Lee', 'quincyl@outlook.com', '555-9012', '777 Oak Blvd', 'Seoul', 'SO', '04547', 'South Korea'), 198 | (20, 'Rachel', 'Harris', 'rachelh@hotmail.com', '555-0123', '888 Pine Blvd', 'Busan', 'BU', '48058', 'South Korea'), 199 | (21, 'Sam', 'Clark', 'samc@gmail.com', '555-1234', '999 Cedar Blvd', 'Incheon', 'IC', '22382', 'South Korea'), 200 | (22, 'Tina', 'Allen', 'tinaallen@yahoo.com', '555-2345', '1010 Redwood Blvd', 'Mexico City', 'CMX', '01000', 'Mexico'), 201 | (23, 'Ursula', 'Scott', 'ursulas@outlook.com', '555-3456', '1111 Willow Blvd', 'Guadalajara', 'JAL', '44100', 'Mexico'), 202 | (24, 'Victor', 'Adams', 'victora@hotmail.com', '555-4567', '1212 Birch Blvd', 'Monterrey', 'NLE', '64000', 'Mexico'), 203 | (25, 'Walter', 'Baker', 'walterb@live.com', '555-5678', '1313 Oak Blvd', 'New York', 'NY', '10001', 'USA'), 204 | (26, 'Xander', 'Nelson', 'xandern@gmail.com', '555-6789', '1414 Cedar Blvd', 'Los Angeles', 'CA', '90001', 'USA'), 205 | (27, 'Yvonne', 'Carter', 'yvonnec@yahoo.com', '555-7890', '1515 Maple Dr', 'Chicago', 'IL', '60601', 'USA'), 206 | (28, 'Zane', 'Mitchell', 'zanem@outlook.com', '555-8901', '1616 Redwood Dr', 'Houston', 'TX', '77001', 'USA'), 207 | (29, 'Anna', 'Roberts', 'annar@hotmail.com', '555-9012', '1717 Willow Dr', 'Sydney', 'NSW', '2000', 'Australia'), 208 | (30, 'Ben', 'King', 'benk@live.com', '555-0123', '1818 Birch Dr', 'Melbourne', 'VIC', '3000', 'Australia'), 209 | (31, 'Chloe', 'Green', 'chloeg@gmail.com', '555-1234', '1919 Oak Dr', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 210 | (32, 'Daniel', 'Evans', 'daniele@yahoo.com', '555-2345', '2020 Cedar Dr', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 211 | (33, 'Ella', 'Collins', 'ellac@outlook.com', '555-3456', '2121 Redwood Dr', 'London', 'England', 'SW1A 1AA', 'UK'), 212 | (34, 'Finn', 'Morris', 'finnm@hotmail.com', '555-4567', '2222 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 213 | (35, 'Grace', 'Lee', 'gracel@live.com', '555-5678', '2323 Birch St', 'Berlin', 'BE', '10117', 'Germany'), 214 | (36, 'Holly', 'Martinez', 'hollym@gmail.com', '555-6789', '2424 Oak St', 'Munich', 'BY', '80331', 'Germany'), 215 | (37, 'Ian', 'Robinson', 'ianr@yahoo.com', '555-7890', '2525 Cedar St', 'Warsaw', 'WA', '00-001', 'Poland'), 216 | (38, 'Jasmine', 'Walker', 'jasminew@outlook.com', '555-8901', '2626 Redwood St', 'Lisbon', 'LI', '1100-148', 'Portugal'), 217 | (39, 'Kyle', 'Young', 'kyley@hotmail.com', '555-9012', '2727 Willow St', 'Pittsburgh', 'PA', '15201','USA'), 218 | (40, 'Liam', 'Harris', 'liamh@live.com', '555-0123', '2828 Birch St', 'Richmond', 'VA', '23220','USA'); 219 | 220 | 221 | CREATE TABLE Orders ( 222 | OrderID INT PRIMARY KEY, 223 | CustomerID INT, 224 | ProductID INT, 225 | OrderDate DATE, 226 | Quantity INT, 227 | TotalAmount DECIMAL(10, 2), 228 | FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID), 229 | FOREIGN KEY (ProductID) REFERENCES Products(ProductID) 230 | ); 231 | 232 | -- Insert 40 orders 233 | INSERT INTO Orders VALUES 234 | (1, 1, 2, '2023-05-14', 1, 800.00), 235 | (2, 1, 3, '2024-09-07', 2, 800.00), 236 | (3, 1, 4, '2022-11-22', 1, 250.00), 237 | (4, 1, 5, '2021-03-30', 3, 150.00), 238 | 239 | (5, 2, 6, '2025-07-19', 1, 30.00), 240 | 241 | (6, 3, 7, '2022-08-25', 2, 300.00), 242 | (7, 3, 8, '2024-06-10', 1, 200.00), 243 | 244 | (8, 4, 9, '2021-12-04', 4, 40.00), 245 | 246 | (9, 5, 10, '2023-02-18', 1, 10.00), 247 | (10, 5, 11, '2025-09-27', 2, 360.00), 248 | 249 | (11, 6, 12, '2023-10-11', 1, 500.00), 250 | (12, 6, 13, '2024-04-03', 1, 25.00), 251 | (13, 6, 14, '2022-07-29', 2, 60.00), 252 | 253 | (14, 7, 15, '2021-01-22', 3, 135.00), 254 | 255 | (15, 8, 16, '2025-11-15', 1, 80.00), 256 | 257 | (16, 9, 17, '2022-10-08', 1, 60.00), 258 | (17, 9, 18, '2023-06-21', 2, 40.00), 259 | (18, 9, 19, '2021-09-13', 5, 50.00), 260 | 261 | (19, 10, 20, '2025-03-05', 2, 50.00), 262 | 263 | (20, 11, 21, '2024-08-14', 1, 60.00), 264 | (21, 11, 22, '2022-12-01', 1, 100.00), 265 | 266 | (22, 12, 23, '2023-09-09', 1, 15.00), 267 | (23, 12, 24, '2021-07-18', 2, 180.00), 268 | 269 | (24, 13, 25, '2025-06-23', 3, 15.00), 270 | 271 | (25, 14, 26, '2023-03-12', 4, 100.00), 272 | (26, 14, 27, '2022-04-07', 1, 450.00), 273 | 274 | (27, 15, 28, '2024-11-30', 1, 600.00), 275 | 276 | (28, 16, 29, '2021-02-25', 1, 500.00), 277 | 278 | (29, 17, 30, '2025-05-28', 2, 240.00), 279 | 280 | (30, 18, 31, '2023-08-20', 1, 350.00), 281 | (31, 18, 32, '2022-01-17', 1, 450.00), 282 | 283 | (32, 19, 33, '2025-09-10', 1, 40.00), 284 | 285 | (33, 20, 34, '2021-04-04', 2, 100.00), 286 | (34, 20, 35, '2024-07-15', 3, 120.00), 287 | (35, 20, 36, '2022-10-31', 1, 60.00), 288 | 289 | (36, 21, 37, '2023-12-22', 1, 35.00), 290 | 291 | (37, 22, 38, '2021-06-06', 2, 110.00), 292 | (38, 22, 39, '2025-02-01', 1, 40.00), 293 | 294 | (39, 23, 40, '2023-11-26', 3, 120.00), 295 | (40, 24, 1, '2024-03-09', 1, 1200.00); 296 | 297 | 298 | DROP TABLE IF EXISTS Sales; 299 | 300 | CREATE TABLE Sales ( 301 | SaleID INT PRIMARY KEY, 302 | ProductID INT, 303 | CustomerID INT, 304 | SaleDate DATE, 305 | SaleAmount DECIMAL(10, 2) 306 | ); 307 | 308 | INSERT INTO Sales (SaleID, ProductID, CustomerID, SaleDate, SaleAmount) VALUES 309 | (1, 1, 1, '2023-01-01', 150.00), 310 | (2, 2, 2, '2023-01-02', 200.00), 311 | (3, 3, 3, '2023-01-03', 250.00), 312 | (4, 4, 4, '2023-01-04', 300.00), 313 | (5, 5, 5, '2023-01-05', 350.00), 314 | (6, 6, 6, '2023-01-06', 400.00), 315 | (7, 7, 7, '2023-01-07', 450.00), 316 | (8, 8, 8, '2023-01-08', 500.00), 317 | (9, 9, 9, '2023-01-09', 550.00), 318 | (10, 10, 10, '2023-01-10', 600.00), 319 | (11, 1, 1, '2023-01-11', 150.00), 320 | (12, 2, 2, '2023-01-12', 200.00), 321 | (13, 3, 3, '2023-01-13', 250.00), 322 | (14, 4, 4, '2023-01-14', 300.00), 323 | (15, 5, 5, '2023-01-15', 350.00), 324 | (16, 6, 6, '2023-01-16', 400.00), 325 | (17, 7, 7, '2023-01-17', 450.00), 326 | (18, 8, 8, '2023-01-18', 500.00), 327 | (19, 9, 9, '2023-01-19', 550.00), 328 | (20, 10, 10, '2023-01-20', 600.00), 329 | (21, 1, 2, '2023-01-21', 150.00), 330 | (22, 2, 3, '2023-01-22', 200.00), 331 | (23, 3, 4, '2023-01-23', 250.00), 332 | (24, 4, 5, '2023-01-24', 300.00), 333 | (25, 5, 6, '2023-01-25', 350.00), 334 | (26, 6, 7, '2023-01-26', 400.00), 335 | (27, 7, 8, '2023-01-27', 450.00), 336 | (28, 8, 9, '2023-01-28', 500.00), 337 | (29, 9, 10, '2023-01-29', 550.00), 338 | (30, 10, 1, '2023-01-30', 600.00), 339 | (31, 1, 2, '2023-02-01', 150.00), 340 | (32, 2, 3, '2023-02-02', 200.00), 341 | (33, 3, 4, '2023-02-03', 250.00), 342 | (34, 4, 5, '2023-02-04', 300.00), 343 | (35, 5, 6, '2023-02-05', 350.00), 344 | (36, 6, 7, '2023-02-06', 400.00), 345 | (37, 7, 8, '2023-02-07', 450.00), 346 | (38, 8, 9, '2023-02-08', 500.00), 347 | (39, 9, 10, '2023-02-09', 550.00), 348 | (40, 10, 1, '2023-02-10', 600.00); 349 | ``` 350 | -------------------------------------------------------------------------------- /lesson-4/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 4: Filtering and Ordering Data 2 | 3 | ✅ Filtering and Ordering Data 4 | ✅ Using SELECT TOP, OFFSET-FETCH 5 | ✅ Using ISNULL and COALESCE 6 | ✅ Order By (ASC, DESC) 7 | ✅ Where Clause Filtering 8 | ✅ LIKE operator 9 | ✅ Wildcard Characters 10 | ✅ ANY and ALL Operators 11 | 12 | 13 | > **Notes before doing the tasks:** 14 | > - Tasks should be solved using **SQL Server**. 15 | > - Case insensitivity applies. 16 | > - Alias names do not affect the score. 17 | > - Scoring is based on the **correct output**. 18 | > - One correct solution is sufficient. 19 | 20 | 21 | ## 🟢 Easy-Level Tasks (10) 22 | 1. Write a query to select the top 5 employees from the Employees table. 23 | 2. Use SELECT DISTINCT to select unique Category values from the Products table. 24 | 3. Write a query that filters the Products table to show products with Price > 100. 25 | 4. Write a query to select all Customers whose FirstName start with 'A' using the LIKE operator. 26 | 5. Order the results of a Products table by Price in ascending order. 27 | 6. Write a query that uses the WHERE clause to filter for employees with Salary >= 60000 and DepartmentName = 'HR'. 28 | 7. Use ISNULL to replace NULL values in the Email column with the text "noemail@example.com".From Employees table 29 | 8. Write a query that shows all products with Price BETWEEN 50 AND 100. 30 | 9. Use SELECT DISTINCT on two columns (Category and ProductName) in the Products table. 31 | 10. After SELECT DISTINCT on two columns (Category and ProductName) Order the results by ProductName in descending order. 32 | 33 | ________________________________________ 34 | 35 | ## 🟠 Medium-Level Tasks (10) 36 | 11. Write a query to select the top 10 products from the Products table, ordered by Price DESC. 37 | 12. Use COALESCE to return the first non-NULL value from FirstName or LastName in the Employees table. 38 | 13. Write a query that selects distinct Category and Price from the Products table. 39 | 14. Write a query that filters the Employees table to show employees whose Age is either between 30 and 40 or DepartmentName = 'Marketing'. 40 | 15. Use OFFSET-FETCH to select rows 11 to 20 from the Employees table, ordered by Salary DESC. 41 | 16. Write a query to display all products with Price <= 1000 and StockQuantity > 50, sorted by Stock in ascending order. 42 | 17. Write a query that filters the Products table for ProductName values containing the letter 'e' using LIKE. 43 | 18. Use IN operator to filter for employees who work in either 'HR', 'IT', or 'Finance'. 44 | 19. Use ORDER BY to display a list of customers ordered by City in ascending and PostalCode in descending order.Use customers table 45 | 46 | ________________________________________ 47 | 48 | ## 🔴 Hard-Level Tasks 49 | 20. Write a query that selects the top 5 products with the highest sales, using TOP(5) and ordered by SaleAmount DESC. 50 | 21. Combine FirstName and LastName into one column named FullName in the Employees table. (only in select statement) 51 | 22. Write a query to select the distinct Category, ProductName, and Price for products that are priced above $50, using DISTINCT on three columns. 52 | 23. Write a query that selects products whose Price is less than 10% of the average price in the Products table. (Do some research on how to find average price of all products) 53 | 24. Use WHERE clause to filter for employees whose Age is less than 30 and who work in either the 'HR' or 'IT' department. 54 | 25. Use LIKE with wildcard to select all customers whose Email contains the domain '@gmail.com'. 55 | 26. Write a query that uses the ALL operator to find employees whose salary is greater than all employees in the 'Sales' department. 56 | 27. Write a query that filters the Orders table for orders placed in the last 180 days using BETWEEN and LATEST_DATE in the table. (Search how to get the current date and latest date) 57 | 58 | 59 | 60 | **Necessary tables:** 61 | ```sql 62 | DROP TABLE IF EXISTS Employees; 63 | 64 | CREATE TABLE Employees ( 65 | EmployeeID INT PRIMARY KEY, 66 | FirstName VARCHAR(50) NULL, 67 | LastName VARCHAR(50) NULL, 68 | DepartmentName VARCHAR(50), 69 | Salary DECIMAL(10, 2), 70 | HireDate DATE, 71 | Age INT, 72 | Email VARCHAR(100) NULL, 73 | Country VARCHAR(50) 74 | ); 75 | 76 | INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentName, Salary, HireDate, Age, Email, Country) VALUES 77 | (1, 'John', 'Doe', 'IT', 55000.00, '2020-01-01', 30, 'johndoe@example.com', 'USA'), 78 | (2, 'Jane', 'Smith', 'HR', 65000.00, '2019-03-15', 28, 'janesmith@example.com', 'USA'), 79 | (3, NULL, 'Johnson', 'Finance', 45000.00, '2021-05-10', 25, NULL, 'Canada'), 80 | (4, 'James', 'Brown', 'Marketing', 60000.00, '2018-07-22', 35, 'jamesbrown@example.com', 'UK'), 81 | (5, 'Patricia', NULL, 'HR', 70000.00, '2017-08-30', 40, NULL, 'USA'), 82 | (6, 'Michael', 'Miller', 'IT', 75000.00, '2020-12-12', 27, 'michaelm@example.com', 'Germany'), 83 | (7, 'Linda', NULL, 'Finance', 48000.00, '2016-11-02', 42, NULL, 'Canada'), 84 | (8, 'David', 'Moore', 'Marketing', 85000.00, '2021-09-01', 29, 'davidm@example.com', 'UK'), 85 | (9, 'Elizabeth', 'Taylor', 'HR', 60000.00, '2019-05-18', 31, 'elizabetht@example.com', 'USA'), 86 | (10, 'William', NULL, 'IT', 64000.00, '2020-04-10', 26, NULL, 'Germany'), 87 | (11, NULL, 'Thomas', 'Finance', 47000.00, '2017-01-25', 38, NULL, 'Canada'), 88 | (12, 'Joseph', 'Jackson', 'Marketing', 78000.00, '2016-09-30', 44, 'josephj@example.com', 'UK'), 89 | (13, 'Karen', 'White', 'HR', 59000.00, '2018-06-10', 33, 'karenw@gmail.com', 'USA'), 90 | (14, 'Steven', NULL, 'IT', 71000.00, '2021-07-15', 24, NULL, 'Germany'), 91 | (15, 'Nancy', 'Clark', 'Finance', 45000.00, '2020-02-20', 27, 'nancyc@example.com', 'Canada'), 92 | (16, 'George', 'Lewis', 'Marketing', 80000.00, '2019-11-10', 36, 'georgel@example.com', 'UK'), 93 | (17, 'Betty', NULL, 'HR', 55000.00, '2017-04-05', 41, NULL, 'USA'), 94 | (18, 'Samuel', 'Walker', 'IT', 72000.00, '2021-03-22', 23, 'samuelw@example.com', 'Germany'), 95 | (19, 'Helen', 'Hall', 'Finance', 49000.00, '2018-10-16', 34, 'helenh@example.com', 'Canada'), 96 | (20, NULL, 'Allen', 'Marketing', 90000.00, '2015-08-11', 50, NULL, 'UK'), 97 | (21, 'Betty', 'Young', 'HR', 53000.00, '2020-05-17', 28, 'bettyy@gmail.com', 'USA'), 98 | (22, 'Frank', NULL, 'IT', 67000.00, '2021-02-02', 26, 'frankk@example.com', 'Germany'), 99 | (23, 'Deborah', 'Scott', 'Finance', 47000.00, '2019-07-09', 29, NULL, 'Canada'), 100 | (24, 'Matthew', 'Green', 'Marketing', 76000.00, '2021-01-15', 30, 'matthewg@example.com', 'UK'), 101 | (25, NULL, 'Adams', 'HR', 54000.00, '2020-06-21', 27, NULL, 'USA'), 102 | (26, 'Paul', 'Nelson', 'IT', 71000.00, '2018-12-03', 37, 'pauln@example.com', 'Germany'), 103 | (27, 'Margaret', NULL, 'Finance', 46000.00, '2020-08-19', 25, 'margaretc@example.com', 'Canada'), 104 | (28, 'Anthony', 'Mitchell', 'Marketing', 82000.00, '2021-04-10', 29, NULL, 'UK'), 105 | (29, 'Lisa', 'Perez', 'HR', 60000.00, '2021-03-05', 24, 'lisap@example.com', 'USA'), 106 | (30, NULL, 'Roberts', 'IT', 69000.00, '2019-09-24', 32, NULL, 'Germany'), 107 | (31, 'Jessica', 'Gonzalez', 'Finance', 47000.00, '2017-12-13', 38, 'jessicag@gamil.com', 'Canada'), 108 | (32, 'Brian', NULL, 'Marketing', 85000.00, '2018-11-05', 35, NULL, 'UK'), 109 | (33, 'Dorothy', 'Evans', 'HR', 59000.00, '2019-06-11', 31, 'dorothye@example.com', 'USA'), 110 | (34, 'Matthew', 'Carter', 'IT', 70000.00, '2020-01-29', 29, 'matthewc@example.com', 'Germany'), 111 | (35, NULL, 'Martinez', 'Finance', 51000.00, '2021-06-06', 22, NULL, 'Canada'), 112 | (36, 'Daniel', 'Perez', 'Marketing', 83000.00, '2021-07-01', 30, 'danielp@example.com', 'UK'), 113 | (37, 'Catherine', 'Roberts', 'HR', 60000.00, '2020-09-19', 27, 'catheriner@gmail.com', 'USA'), 114 | (38, 'Ronald', NULL, 'IT', 68000.00, '2017-02-04', 39, NULL, 'Germany'), 115 | (39, 'Angela', 'Jenkins', 'Finance', 52000.00, '2018-04-23', 34, 'angelaj@example.com', 'Canada'), 116 | (40, 'Gary', 'Wright', 'Marketing', 87000.00, '2021-01-10', 29, NULL, 'UK'); 117 | 118 | 119 | DROP TABLE IF EXISTS Products_Discounted; 120 | 121 | CREATE TABLE Products_Discounted ( 122 | ProductID INT PRIMARY KEY, 123 | ProductName VARCHAR(100), 124 | Price DECIMAL(10, 2), 125 | Category VARCHAR(50), 126 | StockQuantity INT 127 | ); 128 | 129 | INSERT INTO Products_Discounted VALUES 130 | (1, 'Gaming Laptop', 950.00, 'Electronics', 25), 131 | (2, 'High-End Smartphone', 750.00, 'Electronics', 45), 132 | (3, 'Convertible Tablet', 350.00, 'Electronics', 35), 133 | (4, 'Ultra-Wide Monitor', 220.00, 'Electronics', 55), 134 | (5, 'Mechanical Keyboard', 45.00, 'Accessories', 90), 135 | (6, 'Wireless Mouse', 25.00, 'Accessories', 110), 136 | (7, 'Ergonomic Chair', 130.00, 'Furniture', 75), 137 | (8, 'Standing Desk', 190.00, 'Furniture', 70), 138 | (9, 'Luxury Pen', 4.50, 'Stationery', 280), 139 | (10, 'Leather Notebook', 9.00, 'Stationery', 480), 140 | (11, 'Laser Printer', 160.00, 'Electronics', 20), 141 | (12, 'DSLR Camera', 480.00, 'Electronics', 35), 142 | (13, 'LED Flashlight', 20.00, 'Tools', 190), 143 | (14, 'Designer Shirt', 28.00, 'Clothing', 140), 144 | (15, 'Slim Fit Jeans', 40.00, 'Clothing', 110), 145 | (16, 'Winter Jacket', 70.00, 'Clothing', 60), 146 | (17, 'Running Shoes', 55.00, 'Clothing', 90), 147 | (18, 'Wool Hat', 18.00, 'Accessories', 45), 148 | (19, 'Thermal Socks', 9.00, 'Clothing', 180), 149 | (20, 'Graphic T-Shirt', 22.00, 'Clothing', 140), 150 | (21, 'Desk Lamp', 55.00, 'Furniture', 35), 151 | (22, 'Marble Coffee Table', 95.00, 'Furniture', 30), 152 | (23, 'Hardcover Book', 13.00, 'Stationery', 230), 153 | (24, 'Persian Rug', 85.00, 'Furniture', 50), 154 | (25, 'Glass Cup', 4.50, 'Accessories', 470), 155 | (26, 'Leather Bag', 22.00, 'Accessories', 270), 156 | (27, 'Recliner Couch', 420.00, 'Furniture', 10), 157 | (28, 'Smart Fridge', 570.00, 'Electronics', 15), 158 | (29, 'Gas Stove', 460.00, 'Electronics', 12), 159 | (30, 'Compact Microwave', 100.00, 'Electronics', 20), 160 | (31, 'Split Air Conditioner', 320.00, 'Electronics', 8), 161 | (32, 'Front-Load Washing Machine', 410.00, 'Electronics', 12), 162 | (33, 'High-Efficiency Dryer', 370.00, 'Electronics', 8), 163 | (34, 'Ionic Hair Dryer', 27.00, 'Accessories', 90), 164 | (35, 'Steam Iron', 38.00, 'Electronics', 45), 165 | (36, 'Espresso Maker', 45.00, 'Electronics', 55), 166 | (37, 'Portable Blender', 32.00, 'Electronics', 35), 167 | (38, 'Cold Press Juicer', 50.00, 'Electronics', 28), 168 | (39, 'Smart Toaster', 36.00, 'Electronics', 65), 169 | (40, 'Compact Dishwasher', 470.00, 'Electronics', 18); 170 | 171 | 172 | 173 | 174 | 175 | DROP TABLE IF EXISTS Sales; 176 | 177 | CREATE TABLE Sales ( 178 | SaleID INT PRIMARY KEY, 179 | ProductID INT, 180 | CustomerID INT, 181 | SaleDate DATE, 182 | SaleAmount DECIMAL(10, 2) 183 | ); 184 | 185 | INSERT INTO Sales (SaleID, ProductID, CustomerID, SaleDate, SaleAmount) VALUES 186 | (1, 1, 1, '2023-01-01', 150.00), 187 | (2, 2, 2, '2023-01-02', 200.00), 188 | (3, 3, 3, '2023-01-03', 250.00), 189 | (4, 4, 4, '2023-01-04', 300.00), 190 | (5, 5, 5, '2023-01-05', 350.00), 191 | (6, 6, 6, '2023-01-06', 400.00), 192 | (7, 7, 7, '2023-01-07', 450.00), 193 | (8, 8, 8, '2023-01-08', 500.00), 194 | (9, 9, 9, '2023-01-09', 550.00), 195 | (10, 10, 10, '2023-01-10', 600.00), 196 | (11, 1, 1, '2023-01-11', 150.00), 197 | (12, 2, 2, '2023-01-12', 200.00), 198 | (13, 3, 3, '2023-01-13', 250.00), 199 | (14, 4, 4, '2023-01-14', 300.00), 200 | (15, 5, 5, '2023-01-15', 350.00), 201 | (16, 6, 6, '2023-01-16', 400.00), 202 | (17, 7, 7, '2023-01-17', 450.00), 203 | (18, 8, 8, '2023-01-18', 500.00), 204 | (19, 9, 9, '2023-01-19', 550.00), 205 | (20, 10, 10, '2023-01-20', 600.00), 206 | (21, 1, 2, '2023-01-21', 150.00), 207 | (22, 2, 3, '2023-01-22', 200.00), 208 | (23, 3, 4, '2023-01-23', 250.00), 209 | (24, 4, 5, '2023-01-24', 300.00), 210 | (25, 5, 6, '2023-01-25', 350.00), 211 | (26, 6, 7, '2023-01-26', 400.00), 212 | (27, 7, 8, '2023-01-27', 450.00), 213 | (28, 8, 9, '2023-01-28', 500.00), 214 | (29, 9, 10, '2023-01-29', 550.00), 215 | (30, 10, 1, '2023-01-30', 600.00), 216 | (31, 1, 2, '2023-02-01', 150.00), 217 | (32, 2, 3, '2023-02-02', 200.00), 218 | (33, 3, 4, '2023-02-03', 250.00), 219 | (34, 4, 5, '2023-02-04', 300.00), 220 | (35, 5, 6, '2023-02-05', 350.00), 221 | (36, 6, 7, '2023-02-06', 400.00), 222 | (37, 7, 8, '2023-02-07', 450.00), 223 | (38, 8, 9, '2023-02-08', 500.00), 224 | (39, 9, 10, '2023-02-09', 550.00), 225 | (40, 10, 1, '2023-02-10', 600.00); 226 | 227 | 228 | DROP TABLE IF EXISTS Orders; 229 | DROP TABLE IF EXISTS Products; 230 | DROP TABLE IF EXISTS Customers; 231 | 232 | 233 | 234 | CREATE TABLE Products ( 235 | ProductID INT PRIMARY KEY, 236 | ProductName VARCHAR(100), 237 | Price DECIMAL(10, 2), 238 | Category VARCHAR(50), 239 | StockQuantity INT 240 | ); 241 | 242 | INSERT INTO Products VALUES 243 | (1, 'Laptop', 1200.00, 'Electronics', 30), 244 | (2, 'Smartphone', 800.00, 'Electronics', 50), 245 | (3, 'Tablet', 400.00, 'Electronics', 40), 246 | (4, 'Monitor', 250.00, 'Electronics', 60), 247 | (5, 'Keyboard', 50.00, 'Accessories', 100), 248 | (6, 'Mouse', 30.00, 'Accessories', 120), 249 | (7, 'Chair', 150.00, 'Furniture', 80), 250 | (8, 'Desk', 200.00, 'Furniture', 75), 251 | (9, 'Pen', 5.00, 'Stationery', 300), 252 | (10, 'Notebook', 10.00, 'Stationery', 500), 253 | (11, 'Printer', 180.00, 'Electronics', 25), 254 | (12, 'Camera', 500.00, 'Electronics', 40), 255 | (13, 'Flashlight', 25.00, 'Tools', 200), 256 | (14, 'Shirt', 30.00, 'Clothing', 150), 257 | (15, 'Jeans', 45.00, 'Clothing', 120), 258 | (16, 'Jacket', 80.00, 'Clothing', 70), 259 | (17, 'Shoes', 60.00, 'Clothing', 100), 260 | (18, 'Hat', 20.00, 'Accessories', 50), 261 | (19, 'Socks', 10.00, 'Clothing', 200), 262 | (20, 'T-Shirt', 25.00, 'Clothing', 150), 263 | (21, 'Lamp', 60.00, 'Furniture', 40), 264 | (22, 'Coffee Table', 100.00, 'Furniture', 35), 265 | (23, 'Book', 15.00, 'Stationery', 250), 266 | (24, 'Rug', 90.00, 'Furniture', 60), 267 | (25, 'Cup', 5.00, 'Accessories', 500), 268 | (26, 'Bag', 25.00, 'Accessories', 300), 269 | (27, 'Couch', 450.00, 'Furniture', 15), 270 | (28, 'Fridge', 600.00, 'Electronics', 20), 271 | (29, 'Stove', 500.00, 'Electronics', 15), 272 | (30, 'Microwave', 120.00, 'Electronics', 25), 273 | (31, 'Air Conditioner', 350.00, 'Electronics', 10), 274 | (32, 'Washing Machine', 450.00, 'Electronics', 15), 275 | (33, 'Dryer', 400.00, 'Electronics', 10), 276 | (34, 'Hair Dryer', 30.00, 'Accessories', 100), 277 | (35, 'Iron', 40.00, 'Electronics', 50), 278 | (36, 'Coffee Maker', 50.00, 'Electronics', 60), 279 | (37, 'Blender', 35.00, 'Electronics', 40), 280 | (38, 'Juicer', 55.00, 'Electronics', 30), 281 | (39, 'Toaster', 40.00, 'Electronics', 70), 282 | (40, 'Dishwasher', 500.00, 'Electronics', 20); 283 | 284 | 285 | 286 | 287 | 288 | 289 | CREATE TABLE Customers ( 290 | CustomerID INT PRIMARY KEY, 291 | FirstName VARCHAR(100), 292 | LastName VARCHAR(100), 293 | Email VARCHAR(100), 294 | Phone VARCHAR(50), 295 | Address VARCHAR(255), 296 | City VARCHAR(100), 297 | State VARCHAR(100), 298 | PostalCode VARCHAR(20), 299 | Country VARCHAR(100) 300 | ); 301 | 302 | --2. Insert 40 Rows into Customers Table 303 | INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Phone, Address, City, State, PostalCode, Country) VALUES 304 | (1, 'John', 'Doe', 'johndoe@gmail.com', '555-1234', '123 Elm St', 'New York', 'NY', '10001', 'USA'), 305 | (2, 'Jane', 'Smith', 'janesmith@yahoo.com', '555-2345', '456 Oak St', 'Los Angeles', 'CA', '90001', 'USA'), 306 | (3, 'Alice', 'Johnson', 'alicej@outlook.com', '555-3456', '789 Pine St', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 307 | (4, 'Bob', 'Brown', 'bobbrown@hotmail.com', '555-4567', '101 Maple St', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 308 | (5, 'Charlie', 'Davis', 'charliedavis@gmail.com', '555-5678', '202 Birch St', 'Sydney', 'NSW', '2000', 'Australia'), 309 | (6, 'David', 'Martinez', 'davidm@live.com', '555-6789', '303 Cedar St', 'Melbourne', 'VIC', '3000', 'Australia'), 310 | (7, 'Emily', 'Garcia', 'emilyg@yahoo.com', '555-7890', '404 Redwood St', 'London', 'England', 'SW1A 1AA', 'UK'), 311 | (8, 'Frank', 'Hernandez', 'frankh@outlook.com', '555-8901', '505 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 312 | (9, 'Grace', 'Lopez', 'gracel@gmail.com', '555-9012', '606 Aspen St', 'Birmingham', 'England', 'B1 1AA', 'UK'), 313 | (10, 'Helen', 'Gonzalez', 'heleng@yahoo.com', '555-0123', '707 Fir St', 'Berlin', 'BE', '10117', 'Germany'), 314 | (11, 'Irene', 'Perez', 'irenep@hotmail.com', '555-1234', '808 Maple Ave', 'Munich', 'BY', '80331', 'Germany'), 315 | (12, 'Jack', 'Wilson', 'jackw@live.com', '555-2345', '909 Oak Ave', 'Hamburg', 'HH', '20095', 'Germany'), 316 | (13, 'Kim', 'Anderson', 'kima@gmail.com', '555-3456', '111 Pine Ave', 'Paris', '�le-de-France', '75001', 'France'), 317 | (14, 'Liam', 'Thomas', 'liamt@yahoo.com', '555-4567', '222 Cedar Ave', 'Lyon', 'Auvergne-Rh�ne-Alpes', '69001', 'France'), 318 | (15, 'Megan', 'Taylor', 'megant@outlook.com', '555-5678', '333 Redwood Ave', 'Marseille', 'Provence-Alpes-C�te Azur', '13001', 'France'), 319 | (16, 'Nathan', 'Moore', 'nathanm@hotmail.com', '555-6789', '444 Willow Ave', 'Tokyo', 'TK', '100-0001', 'Japan'), 320 | (17, 'Olivia', 'Jackson', 'oliviaj@gmail.com', '555-7890', '555 Birch Ave', 'Osaka', 'OS', '530-0001', 'Japan'), 321 | (18, 'Paul', 'White', 'paulw@yahoo.com', '555-8901', '666 Maple Blvd', 'Kyoto', 'KY', '600-8001', 'Japan'), 322 | (19, 'Quincy', 'Lee', 'quincyl@outlook.com', '555-9012', '777 Oak Blvd', 'Seoul', 'SO', '04547', 'South Korea'), 323 | (20, 'Rachel', 'Harris', 'rachelh@hotmail.com', '555-0123', '888 Pine Blvd', 'Busan', 'BU', '48058', 'South Korea'), 324 | (21, 'Sam', 'Clark', 'samc@gmail.com', '555-1234', '999 Cedar Blvd', 'Incheon', 'IC', '22382', 'South Korea'), 325 | (22, 'Tina', 'Allen', 'tinaallen@yahoo.com', '555-2345', '1010 Redwood Blvd', 'Mexico City', 'CMX', '01000', 'Mexico'), 326 | (23, 'Ursula', 'Scott', 'ursulas@outlook.com', '555-3456', '1111 Willow Blvd', 'Guadalajara', 'JAL', '44100', 'Mexico'), 327 | (24, 'Victor', 'Adams', 'victora@hotmail.com', '555-4567', '1212 Birch Blvd', 'Monterrey', 'NLE', '64000', 'Mexico'), 328 | (25, 'Walter', 'Baker', 'walterb@live.com', '555-5678', '1313 Oak Blvd', 'New York', 'NY', '10001', 'USA'), 329 | (26, 'Xander', 'Nelson', 'xandern@gmail.com', '555-6789', '1414 Cedar Blvd', 'Los Angeles', 'CA', '90001', 'USA'), 330 | (27, 'Yvonne', 'Carter', 'yvonnec@yahoo.com', '555-7890', '1515 Maple Dr', 'Chicago', 'IL', '60601', 'USA'), 331 | (28, 'Zane', 'Mitchell', 'zanem@outlook.com', '555-8901', '1616 Redwood Dr', 'Houston', 'TX', '77001', 'USA'), 332 | (29, 'Anna', 'Roberts', 'annar@hotmail.com', '555-9012', '1717 Willow Dr', 'Sydney', 'NSW', '2000', 'Australia'), 333 | (30, 'Ben', 'King', 'benk@live.com', '555-0123', '1818 Birch Dr', 'Melbourne', 'VIC', '3000', 'Australia'), 334 | (31, 'Chloe', 'Green', 'chloeg@gmail.com', '555-1234', '1919 Oak Dr', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 335 | (32, 'Daniel', 'Evans', 'daniele@yahoo.com', '555-2345', '2020 Cedar Dr', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 336 | (33, 'Ella', 'Collins', 'ellac@outlook.com', '555-3456', '2121 Redwood Dr', 'London', 'England', 'SW1A 1AA', 'UK'), 337 | (34, 'Finn', 'Morris', 'finnm@hotmail.com', '555-4567', '2222 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 338 | (35, 'Grace', 'Lee', 'gracel@live.com', '555-5678', '2323 Birch St', 'Berlin', 'BE', '10117', 'Germany'), 339 | (36, 'Holly', 'Martinez', 'hollym@gmail.com', '555-6789', '2424 Oak St', 'Munich', 'BY', '80331', 'Germany'), 340 | (37, 'Ian', 'Robinson', 'ianr@yahoo.com', '555-7890', '2525 Cedar St', 'Warsaw', 'WA', '00-001', 'Poland'), 341 | (38, 'Jasmine', 'Walker', 'jasminew@outlook.com', '555-8901', '2626 Redwood St', 'Lisbon', 'LI', '1100-148', 'Portugal'), 342 | (39, 'Kyle', 'Young', 'kyley@hotmail.com', '555-9012', '2727 Willow St', 'Pittsburgh', 'PA', '15201','USA'), 343 | (40, 'Liam', 'Harris', 'liamh@live.com', '555-0123', '2828 Birch St', 'Richmond', 'VA', '23220','USA'); 344 | 345 | 346 | CREATE TABLE Orders ( 347 | OrderID INT PRIMARY KEY, 348 | CustomerID INT, 349 | ProductID INT, 350 | OrderDate DATE, 351 | Quantity INT, 352 | TotalAmount DECIMAL(10, 2), 353 | FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID), 354 | FOREIGN KEY (ProductID) REFERENCES Products(ProductID) 355 | ); 356 | 357 | -- Insert 40 rows into Orders with random dates and years 358 | INSERT INTO Orders VALUES 359 | (1, 1, 2, '2023-05-14', 1, 800.00), 360 | (2, 2, 3, '2024-09-07', 2, 800.00), 361 | (3, 3, 4, '2022-11-22', 1, 250.00), 362 | (4, 4, 5, '2021-03-30', 3, 150.00), 363 | (5, 5, 6, '2025-07-19', 1, 30.00), 364 | (6, 6, 7, '2022-08-25', 2, 300.00), 365 | (7, 7, 8, '2024-06-10', 1, 200.00), 366 | (8, 8, 9, '2021-12-04', 4, 40.00), 367 | (9, 9, 10, '2023-02-18', 1, 10.00), 368 | (10, 10, 11, '2025-09-27', 2, 360.00), 369 | (11, 11, 12, '2023-10-11', 1, 500.00), 370 | (12, 12, 13, '2024-04-03', 1, 25.00), 371 | (13, 13, 14, '2022-07-29', 2, 60.00), 372 | (14, 14, 15, '2021-01-22', 3, 135.00), 373 | (15, 15, 16, '2025-11-15', 1, 80.00), 374 | (16, 16, 17, '2022-10-08', 1, 60.00), 375 | (17, 17, 18, '2023-06-21', 2, 40.00), 376 | (18, 18, 19, '2021-09-13', 5, 50.00), 377 | (19, 19, 20, '2025-03-05', 2, 50.00), 378 | (20, 20, 21, '2024-08-14', 1, 60.00), 379 | (21, 21, 22, '2022-12-01', 1, 100.00), 380 | (22, 22, 23, '2023-09-09', 1, 15.00), 381 | (23, 23, 24, '2021-07-18', 2, 180.00), 382 | (24, 24, 25, '2025-06-23', 3, 15.00), 383 | (25, 25, 26, '2023-03-12', 4, 100.00), 384 | (26, 26, 27, '2022-04-07', 1, 450.00), 385 | (27, 27, 28, '2024-11-30', 1, 600.00), 386 | (28, 28, 29, '2021-02-25', 1, 500.00), 387 | (29, 29, 30, '2025-05-28', 2, 240.00), 388 | (30, 30, 31, '2023-08-20', 1, 350.00), 389 | (31, 31, 32, '2022-01-17', 1, 450.00), 390 | (32, 32, 33, '2025-09-10', 1, 40.00), 391 | (33, 33, 34, '2021-04-04', 2, 100.00), 392 | (34, 34, 35, '2024-07-15', 3, 120.00), 393 | (35, 35, 36, '2022-10-31', 1, 60.00), 394 | (36, 36, 37, '2023-12-22', 1, 35.00), 395 | (37, 37, 38, '2021-06-06', 2, 110.00), 396 | (38, 38, 39, '2025-02-01', 1, 40.00), 397 | (39, 39, 40, '2023-11-26', 3, 120.00), 398 | (40, 40, 1, '2024-03-09', 1, 1200.00); 399 | ``` 400 | -------------------------------------------------------------------------------- /lesson-5/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 5: Aliases, Unions, and Conditional columns 2 | All the Tasks should be solved using/in MSSQL-SERVER. 3 | 4 | ✅ Aliases 5 | ✅ UNION, UNION ALL, INTERSECT, EXCEPT 6 | ✅ Creating Conditional Columns with CASE and IIF 7 | ✅ Using IF, WHILE 8 | 9 | 10 | > **Notes before doing the tasks:** 11 | > - Tasks should be solved using **SQL Server**. 12 | > - Case insensitivity applies. 13 | > - Alias names do not affect the score. 14 | > - Scoring is based on the **correct output**. 15 | > - One correct solution is sufficient. 16 | 17 | ________________________________________ 18 | 19 | ## Easy-Level Tasks 20 | 1. Write a query that uses an alias to rename the ProductName column as Name in the Products table. 21 | 2. Write a query that uses an alias to rename the Customers table as Client for easier reference. 22 | 3. Use UNION to combine results from two queries that select ProductName from Products and ProductName from Products_Discounted. 23 | 4. Write a query to find the intersection of Products and Products_Discounted tables using INTERSECT. 24 | 5. Write a query to select distinct customer names and their corresponding Country using SELECT DISTINCT. 25 | 6. Write a query that uses CASE to create a conditional column that displays 'High' if Price > 1000, and 'Low' if Price <= 1000 from Products table. 26 | 7. Use IIF to create a column that shows 'Yes' if StockQuantity > 100, and 'No' otherwise (Products_Discounted table, StockQuantity column). 27 | ________________________________________ 28 | 29 | ## Medium-Level Tasks 30 | 8. Use UNION to combine results from two queries that select ProductName from Products and ProductName from Products_Discounted tables. 31 | 9. Write a query that returns the difference between the Products and Products_Discounted tables using EXCEPT. 32 | 10. Create a conditional column using IIF that shows 'Expensive' if the Price is greater than 1000, and 'Affordable' if less, from Products table. 33 | 11. Write a query to find employees in the Employees table who have either Age < 25 or Salary > 60000. 34 | 12. Update the salary of an employee based on their department, increase by 10% if they work in 'HR' or EmployeeID = 5 35 | ________________________________________ 36 | 37 | ## Hard-Level Tasks 38 | 13. Write a query that uses CASE to assign 'Top Tier' if SaleAmount > 500, 'Mid Tier' if SaleAmount BETWEEN 200 AND 500, and 'Low Tier' otherwise. (From Sales table) 39 | 14. Use EXCEPT to find customers' ID who have placed orders but do not have a corresponding record in the Sales table. 40 | 15. Write a query that uses a CASE statement to determine the discount percentage based on the quantity purchased. Use orders table. Result set should show customerid, quantity and discount percentage. The discount should be applied as follows: 41 | 1 item: 3% 42 | Between 1 and 3 items : 5% 43 | Otherwise: 7% 44 | 45 | **Necessary tables:** 46 | ```sql 47 | CREATE TABLE Products ( 48 | ProductID INT PRIMARY KEY, 49 | ProductName VARCHAR(100), 50 | Price DECIMAL(10, 2), 51 | Category VARCHAR(50), 52 | StockQuantity INT 53 | ); 54 | 55 | INSERT INTO Products VALUES 56 | (1, 'Laptop', 1200.00, 'Electronics', 30), 57 | (2, 'Smartphone', 800.00, 'Electronics', 50), 58 | (3, 'Tablet', 400.00, 'Electronics', 40), 59 | (4, 'Monitor', 250.00, 'Electronics', 60), 60 | (5, 'Keyboard', 50.00, 'Accessories', 100), 61 | (6, 'Mouse', 30.00, 'Accessories', 120), 62 | (7, 'Chair', 150.00, 'Furniture', 80), 63 | (8, 'Desk', 200.00, 'Furniture', 75), 64 | (9, 'Pen', 5.00, 'Stationery', 300), 65 | (10, 'Notebook', 10.00, 'Stationery', 500), 66 | (11, 'Printer', 180.00, 'Electronics', 25), 67 | (12, 'Camera', 500.00, 'Electronics', 40), 68 | (13, 'Flashlight', 25.00, 'Tools', 200), 69 | (14, 'Shirt', 30.00, 'Clothing', 150), 70 | (15, 'Jeans', 45.00, 'Clothing', 120), 71 | (16, 'Jacket', 80.00, 'Clothing', 70), 72 | (17, 'Shoes', 60.00, 'Clothing', 100), 73 | (18, 'Hat', 20.00, 'Accessories', 50), 74 | (19, 'Socks', 10.00, 'Clothing', 200), 75 | (20, 'T-Shirt', 25.00, 'Clothing', 150), 76 | (21, 'Lamp', 60.00, 'Furniture', 40), 77 | (22, 'Coffee Table', 100.00, 'Furniture', 35), 78 | (23, 'Book', 15.00, 'Stationery', 250), 79 | (24, 'Rug', 90.00, 'Furniture', 60), 80 | (25, 'Cup', 5.00, 'Accessories', 500), 81 | (26, 'Bag', 25.00, 'Accessories', 300), 82 | (27, 'Couch', 450.00, 'Furniture', 15), 83 | (28, 'Fridge', 600.00, 'Electronics', 20), 84 | (29, 'Stove', 500.00, 'Electronics', 15), 85 | (30, 'Microwave', 120.00, 'Electronics', 25), 86 | (31, 'Air Conditioner', 350.00, 'Electronics', 10), 87 | (32, 'Washing Machine', 450.00, 'Electronics', 15), 88 | (33, 'Dryer', 400.00, 'Electronics', 10), 89 | (34, 'Hair Dryer', 30.00, 'Accessories', 100), 90 | (35, 'Iron', 40.00, 'Electronics', 50), 91 | (36, 'Coffee Maker', 50.00, 'Electronics', 60), 92 | (37, 'Blender', 35.00, 'Electronics', 40), 93 | (38, 'Juicer', 55.00, 'Electronics', 30), 94 | (39, 'Toaster', 40.00, 'Electronics', 70), 95 | (40, 'Dishwasher', 500.00, 'Electronics', 20); 96 | 97 | CREATE TABLE Customers ( 98 | CustomerID INT PRIMARY KEY, 99 | FirstName VARCHAR(100), 100 | LastName VARCHAR(100), 101 | Email VARCHAR(100), 102 | Phone VARCHAR(50), 103 | Address VARCHAR(255), 104 | City VARCHAR(100), 105 | State VARCHAR(100), 106 | PostalCode VARCHAR(20), 107 | Country VARCHAR(100) 108 | ); 109 | 110 | --2. Insert 40 Rows into Customers Table 111 | INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Phone, Address, City, State, PostalCode, Country) VALUES 112 | (1, 'John', 'Doe', 'johndoe@gmail.com', '555-1234', '123 Elm St', 'New York', 'NY', '10001', 'USA'), 113 | (2, 'Jane', 'Smith', 'janesmith@yahoo.com', '555-2345', '456 Oak St', 'Los Angeles', 'CA', '90001', 'USA'), 114 | (3, 'Alice', 'Johnson', 'alicej@outlook.com', '555-3456', '789 Pine St', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 115 | (4, 'Bob', 'Brown', 'bobbrown@hotmail.com', '555-4567', '101 Maple St', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 116 | (5, 'Charlie', 'Davis', 'charliedavis@gmail.com', '555-5678', '202 Birch St', 'Sydney', 'NSW', '2000', 'Australia'), 117 | (6, 'David', 'Martinez', 'davidm@live.com', '555-6789', '303 Cedar St', 'Melbourne', 'VIC', '3000', 'Australia'), 118 | (7, 'Emily', 'Garcia', 'emilyg@yahoo.com', '555-7890', '404 Redwood St', 'London', 'England', 'SW1A 1AA', 'UK'), 119 | (8, 'Frank', 'Hernandez', 'frankh@outlook.com', '555-8901', '505 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 120 | (9, 'Grace', 'Lopez', 'gracel@gmail.com', '555-9012', '606 Aspen St', 'Birmingham', 'England', 'B1 1AA', 'UK'), 121 | (10, 'Helen', 'Gonzalez', 'heleng@yahoo.com', '555-0123', '707 Fir St', 'Berlin', 'BE', '10117', 'Germany'), 122 | (11, 'Irene', 'Perez', 'irenep@hotmail.com', '555-1234', '808 Maple Ave', 'Munich', 'BY', '80331', 'Germany'), 123 | (12, 'Jack', 'Wilson', 'jackw@live.com', '555-2345', '909 Oak Ave', 'Hamburg', 'HH', '20095', 'Germany'), 124 | (13, 'Kim', 'Anderson', 'kima@gmail.com', '555-3456', '111 Pine Ave', 'Paris', '�le-de-France', '75001', 'France'), 125 | (14, 'Liam', 'Thomas', 'liamt@yahoo.com', '555-4567', '222 Cedar Ave', 'Lyon', 'Auvergne-Rh�ne-Alpes', '69001', 'France'), 126 | (15, 'Megan', 'Taylor', 'megant@outlook.com', '555-5678', '333 Redwood Ave', 'Marseille', 'Provence-Alpes-C�te Azur', '13001', 'France'), 127 | (16, 'Nathan', 'Moore', 'nathanm@hotmail.com', '555-6789', '444 Willow Ave', 'Tokyo', 'TK', '100-0001', 'Japan'), 128 | (17, 'Olivia', 'Jackson', 'oliviaj@gmail.com', '555-7890', '555 Birch Ave', 'Osaka', 'OS', '530-0001', 'Japan'), 129 | (18, 'Paul', 'White', 'paulw@yahoo.com', '555-8901', '666 Maple Blvd', 'Kyoto', 'KY', '600-8001', 'Japan'), 130 | (19, 'Quincy', 'Lee', 'quincyl@outlook.com', '555-9012', '777 Oak Blvd', 'Seoul', 'SO', '04547', 'South Korea'), 131 | (20, 'Rachel', 'Harris', 'rachelh@hotmail.com', '555-0123', '888 Pine Blvd', 'Busan', 'BU', '48058', 'South Korea'), 132 | (21, 'Sam', 'Clark', 'samc@gmail.com', '555-1234', '999 Cedar Blvd', 'Incheon', 'IC', '22382', 'South Korea'), 133 | (22, 'Tina', 'Allen', 'tinaallen@yahoo.com', '555-2345', '1010 Redwood Blvd', 'Mexico City', 'CMX', '01000', 'Mexico'), 134 | (23, 'Ursula', 'Scott', 'ursulas@outlook.com', '555-3456', '1111 Willow Blvd', 'Guadalajara', 'JAL', '44100', 'Mexico'), 135 | (24, 'Victor', 'Adams', 'victora@hotmail.com', '555-4567', '1212 Birch Blvd', 'Monterrey', 'NLE', '64000', 'Mexico'), 136 | (25, 'Walter', 'Baker', 'walterb@live.com', '555-5678', '1313 Oak Blvd', 'New York', 'NY', '10001', 'USA'), 137 | (26, 'Xander', 'Nelson', 'xandern@gmail.com', '555-6789', '1414 Cedar Blvd', 'Los Angeles', 'CA', '90001', 'USA'), 138 | (27, 'Yvonne', 'Carter', 'yvonnec@yahoo.com', '555-7890', '1515 Maple Dr', 'Chicago', 'IL', '60601', 'USA'), 139 | (28, 'Zane', 'Mitchell', 'zanem@outlook.com', '555-8901', '1616 Redwood Dr', 'Houston', 'TX', '77001', 'USA'), 140 | (29, 'Anna', 'Roberts', 'annar@hotmail.com', '555-9012', '1717 Willow Dr', 'Sydney', 'NSW', '2000', 'Australia'), 141 | (30, 'Ben', 'King', 'benk@live.com', '555-0123', '1818 Birch Dr', 'Melbourne', 'VIC', '3000', 'Australia'), 142 | (31, 'Chloe', 'Green', 'chloeg@gmail.com', '555-1234', '1919 Oak Dr', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 143 | (32, 'Daniel', 'Evans', 'daniele@yahoo.com', '555-2345', '2020 Cedar Dr', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 144 | (33, 'Ella', 'Collins', 'ellac@outlook.com', '555-3456', '2121 Redwood Dr', 'London', 'England', 'SW1A 1AA', 'UK'), 145 | (34, 'Finn', 'Morris', 'finnm@hotmail.com', '555-4567', '2222 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 146 | (35, 'Grace', 'Lee', 'gracel@live.com', '555-5678', '2323 Birch St', 'Berlin', 'BE', '10117', 'Germany'), 147 | (36, 'Holly', 'Martinez', 'hollym@gmail.com', '555-6789', '2424 Oak St', 'Munich', 'BY', '80331', 'Germany'), 148 | (37, 'Ian', 'Robinson', 'ianr@yahoo.com', '555-7890', '2525 Cedar St', 'Warsaw', 'WA', '00-001', 'Poland'), 149 | (38, 'Jasmine', 'Walker', 'jasminew@outlook.com', '555-8901', '2626 Redwood St', 'Lisbon', 'LI', '1100-148', 'Portugal'), 150 | (39, 'Kyle', 'Young', 'kyley@hotmail.com', '555-9012', '2727 Willow St', 'Pittsburgh', 'PA', '15201','USA'), 151 | (40, 'Liam', 'Harris', 'liamh@live.com', '555-0123', '2828 Birch St', 'Richmond', 'VA', '23220','USA'); 152 | 153 | 154 | CREATE TABLE Products_Discounted ( 155 | ProductID INT PRIMARY KEY, 156 | ProductName VARCHAR(100), 157 | Price DECIMAL(10, 2), 158 | Category VARCHAR(50), 159 | StockQuantity INT 160 | ); 161 | 162 | INSERT INTO Products_Discounted VALUES 163 | (1, 'Gaming Laptop', 950.00, 'Electronics', 25), 164 | (2, 'Smartphone', 750.00, 'Electronics', 45), 165 | (3, 'Convertible Tablet', 350.00, 'Electronics', 35), 166 | (4, 'Ultra-Wide Monitor', 220.00, 'Electronics', 55), 167 | (5, 'Mechanical Keyboard', 45.00, 'Accessories', 90), 168 | (6, 'Wireless Mouse', 25.00, 'Accessories', 110), 169 | (7, 'Chair', 130.00, 'Furniture', 75), 170 | (8, 'Standing Desk', 190.00, 'Furniture', 70), 171 | (9, 'Luxury Pen', 4.50, 'Stationery', 280), 172 | (10, 'Leather Notebook', 9.00, 'Stationery', 480), 173 | (11, 'Laser Printer', 160.00, 'Electronics', 20), 174 | (12, 'DSLR Camera', 480.00, 'Electronics', 35), 175 | (13, 'LED Flashlight', 20.00, 'Tools', 190), 176 | (14, 'Designer Shirt', 28.00, 'Clothing', 140), 177 | (15, 'Jeans', 40.00, 'Clothing', 110), 178 | (16, 'Winter Jacket', 70.00, 'Clothing', 60), 179 | (17, 'Running Shoes', 55.00, 'Clothing', 90), 180 | (18, 'Wool Hat', 18.00, 'Accessories', 45), 181 | (19, 'Thermal Socks', 9.00, 'Clothing', 180), 182 | (20, 'T-Shirt', 22.00, 'Clothing', 140), 183 | (21, 'Desk Lamp', 55.00, 'Furniture', 35), 184 | (22, 'Marble Coffee Table', 95.00, 'Furniture', 30), 185 | (23, 'Hardcover Book', 13.00, 'Stationery', 230), 186 | (24, 'Persian Rug', 85.00, 'Furniture', 50), 187 | (25, 'Glass Cup', 4.50, 'Accessories', 470), 188 | (26, 'Leather Bag', 22.00, 'Accessories', 270), 189 | (27, 'Recliner Couch', 420.00, 'Furniture', 10), 190 | (28, 'Smart Fridge', 570.00, 'Electronics', 15), 191 | (29, 'Gas Stove', 460.00, 'Electronics', 12), 192 | (30, 'Compact Microwave', 100.00, 'Electronics', 20), 193 | (31, 'Split Air Conditioner', 320.00, 'Electronics', 8), 194 | (32, 'Front-Load Washing Machine', 410.00, 'Electronics', 12), 195 | (33, 'High-Efficiency Dryer', 370.00, 'Electronics', 8), 196 | (34, 'Ionic Hair Dryer', 27.00, 'Accessories', 90), 197 | (35, 'Steam Iron', 38.00, 'Electronics', 45), 198 | (36, 'Espresso Maker', 45.00, 'Electronics', 55), 199 | (37, 'Portable Blender', 32.00, 'Electronics', 35), 200 | (38, 'Cold Press Juicer', 50.00, 'Electronics', 28), 201 | (39, 'Smart Toaster', 36.00, 'Electronics', 65), 202 | (40, 'Compact Dishwasher', 470.00, 'Electronics', 18); 203 | 204 | CREATE TABLE Sales ( 205 | SaleID INT PRIMARY KEY, 206 | ProductID INT, 207 | CustomerID INT, 208 | SaleDate DATE, 209 | SaleAmount DECIMAL(10, 2) 210 | ); 211 | 212 | INSERT INTO Sales (SaleID, ProductID, CustomerID, SaleDate, SaleAmount) VALUES 213 | (1, 1, 1, '2023-01-01', 150.00), 214 | (2, 2, 2, '2023-01-02', 200.00), 215 | (3, 3, 3, '2023-01-03', 250.00), 216 | (4, 4, 4, '2023-01-04', 300.00), 217 | (5, 5, 5, '2023-01-05', 350.00), 218 | (6, 6, 6, '2023-01-06', 400.00), 219 | (7, 7, 7, '2023-01-07', 450.00), 220 | (8, 8, 8, '2023-01-08', 500.00), 221 | (9, 9, 9, '2023-01-09', 550.00), 222 | (10, 10, 10, '2023-01-10', 600.00), 223 | (11, 1, 1, '2023-01-11', 150.00), 224 | (12, 2, 2, '2023-01-12', 200.00), 225 | (13, 3, 3, '2023-01-13', 250.00), 226 | (14, 4, 4, '2023-01-14', 300.00), 227 | (15, 5, 5, '2023-01-15', 350.00), 228 | (16, 6, 6, '2023-01-16', 400.00), 229 | (17, 7, 7, '2023-01-17', 450.00), 230 | (18, 8, 8, '2023-01-18', 500.00), 231 | (19, 9, 9, '2023-01-19', 550.00), 232 | (20, 10, 10, '2023-01-20', 600.00), 233 | (21, 1, 2, '2023-01-21', 150.00), 234 | (22, 2, 3, '2023-01-22', 200.00), 235 | (23, 3, 4, '2023-01-23', 250.00), 236 | (24, 4, 5, '2023-01-24', 300.00), 237 | (25, 5, 6, '2023-01-25', 350.00), 238 | (26, 6, 7, '2023-01-26', 400.00), 239 | (27, 7, 8, '2023-01-27', 450.00), 240 | (28, 8, 9, '2023-01-28', 500.00), 241 | (29, 9, 10, '2023-01-29', 550.00), 242 | (30, 10, 1, '2023-01-30', 600.00), 243 | (31, 1, 2, '2023-02-01', 150.00), 244 | (32, 2, 3, '2023-02-02', 200.00), 245 | (33, 3, 4, '2023-02-03', 250.00), 246 | (34, 4, 5, '2023-02-04', 300.00), 247 | (35, 5, 6, '2023-02-05', 350.00), 248 | (36, 6, 7, '2023-02-06', 400.00), 249 | (37, 7, 8, '2023-02-07', 450.00), 250 | (38, 8, 9, '2023-02-08', 500.00), 251 | (39, 9, 10, '2023-02-09', 550.00), 252 | (40, 10, 1, '2023-02-10', 600.00); 253 | 254 | CREATE TABLE Orders ( 255 | OrderID INT PRIMARY KEY, 256 | CustomerID INT, 257 | ProductID INT, 258 | OrderDate DATE, 259 | Quantity INT, 260 | TotalAmount DECIMAL(10, 2), 261 | FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID), 262 | FOREIGN KEY (ProductID) REFERENCES Products(ProductID) 263 | ); 264 | 265 | -- Insert 40 rows into Orders with random dates and years 266 | INSERT INTO Orders VALUES 267 | (1, 1, 2, '2023-05-14', 1, 800.00), 268 | (2, 2, 3, '2024-09-07', 2, 800.00), 269 | (3, 3, 4, '2022-11-22', 1, 250.00), 270 | (4, 4, 5, '2021-03-30', 3, 150.00), 271 | (5, 5, 6, '2025-07-19', 1, 30.00), 272 | (6, 6, 7, '2022-08-25', 2, 300.00), 273 | (7, 7, 8, '2024-06-10', 1, 200.00), 274 | (8, 8, 9, '2021-12-04', 4, 40.00), 275 | (9, 9, 10, '2023-02-18', 1, 10.00), 276 | (10, 10, 11, '2025-09-27', 2, 360.00), 277 | (11, 11, 12, '2023-10-11', 1, 500.00), 278 | (12, 12, 13, '2024-04-03', 1, 25.00), 279 | (13, 13, 14, '2022-07-29', 2, 60.00), 280 | (14, 14, 15, '2021-01-22', 3, 135.00), 281 | (15, 15, 16, '2025-11-15', 1, 80.00), 282 | (16, 16, 17, '2022-10-08', 1, 60.00), 283 | (17, 17, 18, '2023-06-21', 2, 40.00), 284 | (18, 18, 19, '2021-09-13', 5, 50.00), 285 | (19, 19, 20, '2025-03-05', 2, 50.00), 286 | (20, 20, 21, '2024-08-14', 1, 60.00), 287 | (21, 21, 22, '2022-12-01', 1, 100.00), 288 | (22, 22, 23, '2023-09-09', 1, 15.00), 289 | (23, 23, 24, '2021-07-18', 2, 180.00), 290 | (24, 24, 25, '2025-06-23', 3, 15.00), 291 | (25, 25, 26, '2023-03-12', 4, 100.00), 292 | (26, 26, 27, '2022-04-07', 1, 450.00), 293 | (27, 27, 28, '2024-11-30', 1, 600.00), 294 | (28, 28, 29, '2021-02-25', 1, 500.00), 295 | (29, 29, 30, '2025-05-28', 2, 240.00), 296 | (30, 30, 31, '2023-08-20', 1, 350.00), 297 | (31, 31, 32, '2022-01-17', 1, 450.00), 298 | (32, 32, 33, '2025-09-10', 1, 40.00), 299 | (33, 33, 34, '2021-04-04', 2, 100.00), 300 | (34, 34, 35, '2024-07-15', 3, 120.00), 301 | (35, 35, 36, '2022-10-31', 1, 60.00), 302 | (36, 36, 37, '2023-12-22', 1, 35.00), 303 | (37, 37, 38, '2021-06-06', 2, 110.00), 304 | (38, 38, 39, '2025-02-01', 1, 40.00), 305 | (39, 39, 40, '2023-11-26', 3, 120.00), 306 | (40, 40, 1, '2024-03-09', 1, 1200.00); 307 | 308 | 309 | CREATE TABLE Invoices ( 310 | InvoiceID INT PRIMARY KEY, 311 | CustomerID INT, 312 | InvoiceDate DATE, 313 | TotalAmount DECIMAL(10, 2) 314 | ); 315 | 316 | INSERT INTO Invoices (InvoiceID, CustomerID, InvoiceDate, TotalAmount) VALUES 317 | (1, 1, '2023-01-05', 150.00), 318 | (2, 2, '2023-01-07', 200.00), 319 | (3, 3, '2023-01-10', 250.00), 320 | (4, 4, '2023-01-12', 300.00), 321 | (5, 5, '2023-01-15', 350.00), 322 | (6, 6, '2023-01-18', 400.00), 323 | (7, 7, '2023-01-20', 450.00), 324 | (8, 8, '2023-01-23', 500.00), 325 | (9, 9, '2023-01-25', 550.00), 326 | (10, 10, '2023-01-28', 600.00), 327 | (11, 11, '2023-02-02', 150.00), 328 | (12, 12, '2023-02-04', 200.00), 329 | (13, 13, '2023-02-07', 250.00), 330 | (14, 14, '2023-02-09', 300.00), 331 | (15, 15, '2023-02-11', 350.00), 332 | (16, 16, '2023-02-13', 400.00), 333 | (17, 17, '2023-02-15', 450.00), 334 | (18, 18, '2023-02-17', 500.00), 335 | (19, 19, '2023-02-19', 550.00), 336 | (20, 20, '2023-02-21', 600.00), 337 | (21, 21, '2023-02-24', 150.00), 338 | (22, 22, '2023-02-26', 200.00), 339 | (23, 23, '2023-02-28', 250.00), 340 | (24, 24, '2023-03-02', 300.00), 341 | (25, 25, '2023-03-04', 350.00), 342 | (26, 26, '2023-03-06', 400.00), 343 | (27, 27, '2023-03-08', 450.00), 344 | (28, 28, '2023-03-10', 500.00), 345 | (29, 29, '2023-03-12', 550.00), 346 | (30, 30, '2023-03-14', 600.00), 347 | (31, 31, '2023-03-17', 150.00), 348 | (32, 32, '2023-03-19', 200.00), 349 | (33, 33, '2023-03-21', 250.00), 350 | (34, 34, '2023-03-23', 300.00), 351 | (35, 35, '2023-03-25', 350.00), 352 | (36, 36, '2023-03-27', 400.00), 353 | (37, 37, '2023-03-29', 450.00), 354 | (38, 38, '2023-03-31', 500.00), 355 | (39, 39, '2023-04-02', 550.00), 356 | (40, 40, '2023-04-04', 600.00); 357 | 358 | CREATE TABLE OutOfStock ( 359 | ProductID INT PRIMARY KEY, 360 | ProductName VARCHAR(100), 361 | Price DECIMAL(10, 2), 362 | Category VARCHAR(50), 363 | StockQuantity INT 364 | ); 365 | 366 | 367 | INSERT INTO OutOfStock VALUES 368 | (1, 'Gaming Console', 500.00, 'Electronics', 0), 369 | (2, 'Smartwatch', 250.00, 'Electronics', 0), 370 | (3, 'Wireless Earbuds', 150.00, 'Electronics', 0), 371 | (4, 'Projector', 700.00, 'Electronics', 0), 372 | (5, 'Mechanical Keyboard', 120.00, 'Accessories', 0), 373 | (6, 'Wireless Mouse', 45.00, 'Accessories', 0), 374 | (7, 'Office Chair', 250.00, 'Furniture', 0), 375 | (8, 'Standing Desk', 400.00, 'Furniture', 0), 376 | (9, 'Marker Set', 20.00, 'Stationery', 0), 377 | (10, 'Sketchbook', 35.00, 'Stationery', 0), 378 | (11, 'Scanner', 220.00, 'Electronics', 0), 379 | (12, 'Drone', 800.00, 'Electronics', 0), 380 | (13, 'Power Drill', 90.00, 'Tools', 0), 381 | (14, 'Sweater', 55.00, 'Clothing', 0), 382 | (15, 'Shorts', 30.00, 'Clothing', 0), 383 | (16, 'Raincoat', 75.00, 'Clothing', 0), 384 | (17, 'Sandals', 40.00, 'Clothing', 0), 385 | (18, 'Gloves', 15.00, 'Accessories', 0), 386 | (19, 'Necklace', 120.00, 'Accessories', 0), 387 | (20, 'Sunglasses', 80.00, 'Accessories', 0), 388 | (21, 'Bedside Lamp', 45.00, 'Furniture', 0), 389 | (22, 'Bookshelf', 150.00, 'Furniture', 0), 390 | (23, 'Dictionary', 25.00, 'Stationery', 0), 391 | (24, 'Wall Clock', 60.00, 'Furniture', 0), 392 | (25, 'Thermos', 35.00, 'Accessories', 0), 393 | (26, 'Backpack', 60.00, 'Accessories', 0), 394 | (27, 'Recliner', 550.00, 'Furniture', 0), 395 | (28, 'Freezer', 750.00, 'Electronics', 0), 396 | (29, 'Induction Cooktop', 300.00, 'Electronics', 0), 397 | (30, 'Oven', 600.00, 'Electronics', 0), 398 | (31, 'Humidifier', 90.00, 'Electronics', 0), 399 | (32, 'Vacuum Cleaner', 250.00, 'Electronics', 0), 400 | (33, 'Electric Kettle', 45.00, 'Electronics', 0), 401 | (34, 'Smart Light Bulb', 30.00, 'Accessories', 0), 402 | (35, 'Water Purifier', 120.00, 'Electronics', 0), 403 | (36, 'Popcorn Maker', 50.00, 'Electronics', 0), 404 | (37, 'Rice Cooker', 70.00, 'Electronics', 0), 405 | (38, 'Food Processor', 90.00, 'Electronics', 0), 406 | (39, 'Deep Fryer', 80.00, 'Electronics', 0), 407 | (40, 'Robot Vacuum', 500.00, 'Electronics', 0); 408 | 409 | CREATE TABLE Employees ( 410 | EmployeeID INT PRIMARY KEY, 411 | FirstName VARCHAR(50) NULL, 412 | LastName VARCHAR(50) NULL, 413 | DepartmentName VARCHAR(50), 414 | Salary DECIMAL(10, 2), 415 | HireDate DATE, 416 | Age INT, 417 | Email VARCHAR(100) NULL, 418 | Country VARCHAR(50) 419 | ); 420 | 421 | INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentName, Salary, HireDate, Age, Email, Country) VALUES 422 | (1, 'John', 'Doe', 'IT', 55000.00, '2020-01-01', 30, 'johndoe@example.com', 'USA'), 423 | (2, 'Jane', 'Smith', 'HR', 65000.00, '2019-03-15', 28, 'janesmith@example.com', 'USA'), 424 | (3, NULL, 'Johnson', 'Finance', 45000.00, '2021-05-10', 25, NULL, 'Canada'), 425 | (4, 'James', 'Brown', 'Marketing', 60000.00, '2018-07-22', 35, 'jamesbrown@example.com', 'UK'), 426 | (5, 'Patricia', NULL, 'HR', 70000.00, '2017-08-30', 40, NULL, 'USA'), 427 | (6, 'Michael', 'Miller', 'IT', 75000.00, '2020-12-12', 27, 'michaelm@example.com', 'Germany'), 428 | (7, 'Linda', NULL, 'Finance', 48000.00, '2016-11-02', 42, NULL, 'Canada'), 429 | (8, 'David', 'Moore', 'Marketing', 85000.00, '2021-09-01', 29, 'davidm@example.com', 'UK'), 430 | (9, 'Elizabeth', 'Taylor', 'HR', 60000.00, '2019-05-18', 31, 'elizabetht@example.com', 'USA'), 431 | (10, 'William', NULL, 'IT', 64000.00, '2020-04-10', 26, NULL, 'Germany'), 432 | (11, NULL, 'Thomas', 'Finance', 47000.00, '2017-01-25', 38, NULL, 'Canada'), 433 | (12, 'Joseph', 'Jackson', 'Marketing', 78000.00, '2016-09-30', 44, 'josephj@example.com', 'UK'), 434 | (13, 'Karen', 'White', 'HR', 59000.00, '2018-06-10', 33, 'karenw@example.com', 'USA'), 435 | (14, 'Steven', NULL, 'IT', 71000.00, '2021-07-15', 24, NULL, 'Germany'), 436 | (15, 'Nancy', 'Clark', 'Finance', 45000.00, '2020-02-20', 27, 'nancyc@example.com', 'Canada'), 437 | (16, 'George', 'Lewis', 'Marketing', 80000.00, '2019-11-10', 36, 'georgel@example.com', 'UK'), 438 | (17, 'Betty', NULL, 'HR', 55000.00, '2017-04-05', 41, NULL, 'USA'), 439 | (18, 'Samuel', 'Walker', 'IT', 72000.00, '2021-03-22', 23, 'samuelw@example.com', 'Germany'), 440 | (19, 'Helen', 'Hall', 'Finance', 49000.00, '2018-10-16', 34, 'helenh@example.com', 'Canada'), 441 | (20, NULL, 'Allen', 'Marketing', 90000.00, '2015-08-11', 50, NULL, 'UK'), 442 | (21, 'Betty', 'Young', 'HR', 53000.00, '2020-05-17', 28, 'bettyy@example.com', 'USA'), 443 | (22, 'Frank', NULL, 'IT', 67000.00, '2021-02-02', 26, 'frankk@example.com', 'Germany'), 444 | (23, 'Deborah', 'Scott', 'Finance', 47000.00, '2019-07-09', 29, NULL, 'Canada'), 445 | (24, 'Matthew', 'Green', 'Marketing', 76000.00, '2021-01-15', 30, 'matthewg@example.com', 'UK'), 446 | (25, NULL, 'Adams', 'HR', 54000.00, '2020-06-21', 27, NULL, 'USA'), 447 | (26, 'Paul', 'Nelson', 'IT', 71000.00, '2018-12-03', 37, 'pauln@example.com', 'Germany'), 448 | (27, 'Margaret', NULL, 'Finance', 46000.00, '2020-08-19', 25, 'margaretc@example.com', 'Canada'), 449 | (28, 'Anthony', 'Mitchell', 'Marketing', 82000.00, '2021-04-10', 29, NULL, 'UK'), 450 | (29, 'Lisa', 'Perez', 'HR', 60000.00, '2021-03-05', 24, 'lisap@example.com', 'USA'), 451 | (30, NULL, 'Roberts', 'IT', 69000.00, '2019-09-24', 32, NULL, 'Germany'), 452 | (31, 'Jessica', 'Gonzalez', 'Finance', 47000.00, '2017-12-13', 38, 'jessicag@example.com', 'Canada'), 453 | (32, 'Brian', NULL, 'Marketing', 85000.00, '2018-11-05', 35, NULL, 'UK'), 454 | (33, 'Dorothy', 'Evans', 'HR', 59000.00, '2019-06-11', 31, 'dorothye@example.com', 'USA'), 455 | (34, 'Matthew', 'Carter', 'IT', 70000.00, '2020-01-29', 29, 'matthewc@example.com', 'Germany'), 456 | (35, NULL, 'Martinez', 'Finance', 51000.00, '2021-06-06', 22, NULL, 'Canada'), 457 | (36, 'Daniel', 'Perez', 'Marketing', 83000.00, '2021-07-01', 30, 'danielp@example.com', 'UK'), 458 | (37, 'Catherine', 'Roberts', 'HR', 60000.00, '2020-09-19', 27, 'catheriner@example.com', 'USA'), 459 | (38, 'Ronald', NULL, 'IT', 68000.00, '2017-02-04', 39, NULL, 'Germany'), 460 | (39, 'Angela', 'Jenkins', 'Finance', 52000.00, '2018-04-23', 34, 'angelaj@example.com', 'Canada'), 461 | (40, 'Gary', 'Wright', 'Marketing', 87000.00, '2021-01-10', 29, NULL, 'UK'); 462 | ``` 463 | -------------------------------------------------------------------------------- /lesson-9/homework/homework.md: -------------------------------------------------------------------------------- 1 | # Lesson 9 Joins (Only inner with table relationships) 2 | 3 | These homework tasks cover the following topics: 4 | - **Table Relationships** 5 | • One to One 6 | • One to Many 7 | • Many to Many 8 | - **Cartesian Product (Cross join)** 9 | - **Filtering Cartesian product with Where clause** 10 | - **Showing INNER join** 11 | - **Using different logical Operators in ON clause(=, <>, >, >=, <, <=)** 12 | 13 | > **Notes before doing the tasks:** 14 | > - Tasks should be solved using **SQL Server**. 15 | > - Case insensitivity applies. 16 | > - Alias names do not affect the score. 17 | > - Scoring is based on the **correct output**. 18 | > - One correct solution is sufficient. 19 | 20 | 21 | ## 🟢 Easy-Level Tasks (10) 22 | 23 | 🟢 Easy (10 puzzles) 24 | 1. Using Products, Suppliers table 25 | List all combinations of product names and supplier names. 26 | 2. Using Departments, Employees table 27 | Get all combinations of departments and employees. 28 | 3. Using Products, Suppliers table 29 | List only the combinations where the supplier actually supplies the product. Return supplier name and product name 30 | 4. Using Orders, Customers table 31 | List customer names and their orders ID. 32 | 5. Using Courses, Students table 33 | Get all combinations of students and courses. 34 | 6. Using Products, Orders table 35 | Get product names and orders where product IDs match. 36 | 7. Using Departments, Employees table 37 | List employees whose DepartmentID matches the department. 38 | 8. Using Students, Enrollments table 39 | List student names and their enrolled course IDs. 40 | 9. Using Payments, Orders table 41 | List all orders that have matching payments. 42 | 10. Using Orders, Products table 43 | Show orders where product price is more than 100. 44 | 45 | ## 🟡 Medium (10 puzzles) 46 | 11. Using Employees, Departments table List employee names and department names where department IDs are not equal. It means: Show all mismatched employee-department combinations. 47 | 12. Using Orders, Products table Show orders where ordered quantity is greater than stock quantity. 48 | 13. Using Customers, Sales table List customer names and product IDs where sale amount is 500 or more. 49 | 14. Using Courses, Enrollments, Students table List student names and course names they’re enrolled in. 50 | 15. Using Products, Suppliers table List product and supplier names where supplier name contains “Tech”. 51 | 16. Using Orders, Payments table Show orders where payment amount is less than total amount. 52 | 17. Using Employees and Departments tables, get the Department Name for each employee. 53 | 18. Using Products, Categories table Show products where category is either 'Electronics' or 'Furniture'. 54 | 19. Using Sales, Customers table Show all sales from customers who are from 'USA'. 55 | 20. Using Orders, Customers table List orders made by customers from 'Germany' and order total > 100. 56 | 57 | ## 🔴 Hard (5 puzzles)(Do some research for the tasks below) 58 | 21. Using Employees table List all pairs of employees from different departments. 59 | 22. Using Payments, Orders, Products table List payment details where the paid amount is not equal to (Quantity × Product Price). 60 | 23. Using Students, Enrollments, Courses table Find students who are not enrolled in any course. 61 | 24. Using Employees table List employees who are managers of someone, but their salary is less than or equal to the person they manage. 62 | 25. Using Orders, Payments, Customers table List customers who have made an order, but no payment has been recorded for it. 63 | 64 | --- 65 | ```sql 66 | DROP TABLE IF EXISTS Employees; 67 | 68 | CREATE TABLE Employees ( 69 | EmployeeID INT PRIMARY KEY, 70 | Name VARCHAR(100), 71 | DepartmentID INT, 72 | Salary DECIMAL(10, 2), 73 | HireDate DATE, 74 | ManagerID INT, 75 | FOREIGN KEY (ManagerID) REFERENCES Employees(EmployeeID) 76 | ); 77 | 78 | INSERT INTO Employees (EmployeeID, Name, DepartmentID, Salary, HireDate, ManagerID) VALUES 79 | (1, 'John Doe', 1, 55000.00, '2020-01-01', NULL), 80 | (2, 'Jane Smith', 2, 65000.00, '2019-03-15', 1), 81 | (3, 'Mary Johnson', 3, 45000.00, '2021-05-10', 2), 82 | (4, 'James Brown', 5, 60000.00, '2018-07-22', 1), 83 | (5, 'Patricia Davis', 4, 70000.00, '2017-08-30', 4), 84 | (6, 'Michael Miller', 2, 75000.00, '2020-12-12', 2), 85 | (7, 'Linda Wilson', 3, 48000.00, '2016-11-02', 3), 86 | (8, 'David Moore', 4, 85000.00, '2021-09-01', 5), 87 | (9, 'Elizabeth Taylor', 1, 60000.00, '2019-05-18', 1), 88 | (10, 'William Anderson', 2, 64000.00, '2020-04-10', 2), 89 | (11, 'Susan Thomas', 3, 47000.00, '2017-01-25', 3), 90 | (12, 'Joseph Jackson', 4, 78000.00, '2016-09-30', 5), 91 | (13, 'Karen White', 1, 59000.00, '2018-06-10', 9), 92 | (14, 'Steven Harris', 2, 71000.00, '2021-07-15', 6), 93 | (15, 'Nancy Clark', 3, 45000.00, '2020-02-20', 7), 94 | (16, 'George Lewis', 4, 80000.00, '2019-11-10', 8), 95 | (17, 'Betty Lee', 5, 55000.00, '2017-04-05', 4), 96 | (18, 'Samuel Walker', 2, 72000.00, '2021-03-22', 6), 97 | (19, 'Helen Hall', 3, 49000.00, '2018-10-16', 7), 98 | (20, 'Charles Allen', 4, 90000.00, '2015-08-11', 12), 99 | (21, 'Betty Young', 1, 53000.00, '2020-05-17', 9), 100 | (22, 'Frank King', 2, 67000.00, '2021-02-02', 10), 101 | (23, 'Deborah Scott', 3, 47000.00, '2019-07-09', 11), 102 | (24, 'Matthew Green', 4, 76000.00, '2021-01-15', 16), 103 | (25, 'Sandra Adams', 1, 54000.00, '2020-06-21', 21), 104 | (26, 'Paul Nelson', 2, 71000.00, '2018-12-03', 10), 105 | (27, 'Margaret Carter', 3, 46000.00, '2020-08-19', 19), 106 | (28, 'Anthony Mitchell', 4, 82000.00, '2021-04-10', 16), 107 | (29, 'Lisa Perez', 1, 60000.00, '2021-03-05', 21), 108 | (30, 'Christopher Roberts', 2, 69000.00, '2019-09-24', 22), 109 | (31, 'Jessica Gonzalez', 3, 47000.00, '2017-12-13', 23), 110 | (32, 'Brian Moore', 5, 85000.00, '2018-11-05', 17), 111 | (33, 'Dorothy Evans', 1, 59000.00, '2019-06-11', 25), 112 | (34, 'Matthew Carter', 2, 70000.00, '2020-01-29', 26), 113 | (35, 'Rachel Martinez', 3, 51000.00, '2021-06-06', 27), 114 | (36, 'Daniel Perez', 4, 83000.00, '2021-07-01', 28), 115 | (37, 'Catherine Roberts', 1, 60000.00, '2020-09-19', 29), 116 | (38, 'Ronald Murphy', 2, 68000.00, '2017-02-04', 30), 117 | (39, 'Angela Jenkins', 3, 52000.00, '2018-04-23', 31), 118 | (40, 'Gary Wright', 4, 87000.00, '2021-01-10', 36), 119 | (41, 'Kevin Turner', 6, 55000.00, '2022-02-14', NULL), 120 | (42, 'Laura Phillips', 7, 62000.00, '2023-05-22', 41), 121 | (43, 'Eric Collins', 8, 58000.00, '2020-10-30', 41), 122 | (44, 'Megan Edwards', 9, 70000.00, '2019-11-25', 41); 123 | 124 | DROP TABLE IF EXISTS Departments; 125 | 126 | CREATE TABLE Departments ( 127 | DepartmentID INT PRIMARY KEY, 128 | DepartmentName VARCHAR(100) 129 | ); 130 | 131 | INSERT INTO Departments (DepartmentID, DepartmentName) VALUES 132 | (1, 'Sales'), 133 | (2, 'Human Resources'), 134 | (3, 'IT'), 135 | (4, 'Marketing'), 136 | (5, 'Finance'); 137 | 138 | DROP TABLE IF EXISTS Customers; 139 | 140 | --1. Create Customers Table 141 | CREATE TABLE Customers ( 142 | CustomerID INT PRIMARY KEY, 143 | FirstName VARCHAR(100), 144 | LastName VARCHAR(100), 145 | Email VARCHAR(100), 146 | Phone VARCHAR(50), 147 | Address VARCHAR(255), 148 | City VARCHAR(100), 149 | State VARCHAR(100), 150 | PostalCode VARCHAR(20), 151 | Country VARCHAR(100) 152 | ); 153 | 154 | --2. Insert 40 Rows into Customers Table 155 | INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Phone, Address, City, State, PostalCode, Country) VALUES 156 | (1, 'John', 'Doe', 'johndoe@gmail.com', '555-1234', '123 Elm St', 'New York', 'NY', '10001', 'USA'), 157 | (2, 'Jane', 'Smith', 'janesmith@yahoo.com', '555-2345', '456 Oak St', 'Los Angeles', 'CA', '90001', 'USA'), 158 | (3, 'Alice', 'Johnson', 'alicej@outlook.com', '555-3456', '789 Pine St', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 159 | (4, 'Bob', 'Brown', 'bobbrown@hotmail.com', '555-4567', '101 Maple St', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 160 | (5, 'Charlie', 'Davis', 'charliedavis@gmail.com', '555-5678', '202 Birch St', 'Sydney', 'NSW', '2000', 'Australia'), 161 | (6, 'David', 'Martinez', 'davidm@live.com', '555-6789', '303 Cedar St', 'Melbourne', 'VIC', '3000', 'Australia'), 162 | (7, 'Emily', 'Garcia', 'emilyg@yahoo.com', '555-7890', '404 Redwood St', 'London', 'England', 'SW1A 1AA', 'UK'), 163 | (8, 'Frank', 'Hernandez', 'frankh@outlook.com', '555-8901', '505 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 164 | (9, 'Grace', 'Lopez', 'gracel@gmail.com', '555-9012', '606 Aspen St', 'Birmingham', 'England', 'B1 1AA', 'UK'), 165 | (10, 'Helen', 'Gonzalez', 'heleng@yahoo.com', '555-0123', '707 Fir St', 'Berlin', 'BE', '10117', 'Germany'), 166 | (11, 'Irene', 'Perez', 'irenep@hotmail.com', '555-1234', '808 Maple Ave', 'Munich', 'BY', '80331', 'Germany'), 167 | (12, 'Jack', 'Wilson', 'jackw@live.com', '555-2345', '909 Oak Ave', 'Hamburg', 'HH', '20095', 'Germany'), 168 | (13, 'Kim', 'Anderson', 'kima@gmail.com', '555-3456', '111 Pine Ave', 'Paris', 'Île-de-France', '75001', 'France'), 169 | (14, 'Liam', 'Thomas', 'liamt@yahoo.com', '555-4567', '222 Cedar Ave', 'Lyon', 'Auvergne-Rhône-Alpes', '69001', 'France'), 170 | (15, 'Megan', 'Taylor', 'megant@outlook.com', '555-5678', '333 Redwood Ave', 'Marseille', 'Provence-Alpes-Côte Azur', '13001', 'France'), 171 | (16, 'Nathan', 'Moore', 'nathanm@hotmail.com', '555-6789', '444 Willow Ave', 'Tokyo', 'TK', '100-0001', 'Japan'), 172 | (17, 'Olivia', 'Jackson', 'oliviaj@gmail.com', '555-7890', '555 Birch Ave', 'Osaka', 'OS', '530-0001', 'Japan'), 173 | (18, 'Paul', 'White', 'paulw@yahoo.com', '555-8901', '666 Maple Blvd', 'Kyoto', 'KY', '600-8001', 'Japan'), 174 | (19, 'Quincy', 'Lee', 'quincyl@outlook.com', '555-9012', '777 Oak Blvd', 'Seoul', 'SO', '04547', 'South Korea'), 175 | (20, 'Rachel', 'Harris', 'rachelh@hotmail.com', '555-0123', '888 Pine Blvd', 'Busan', 'BU', '48058', 'South Korea'), 176 | (21, 'Sam', 'Clark', 'samc@gmail.com', '555-1234', '999 Cedar Blvd', 'Incheon', 'IC', '22382', 'South Korea'), 177 | (22, 'Tina', 'Allen', 'tinaallen@yahoo.com', '555-2345', '1010 Redwood Blvd', 'Mexico City', 'CMX', '01000', 'Mexico'), 178 | (23, 'Ursula', 'Scott', 'ursulas@outlook.com', '555-3456', '1111 Willow Blvd', 'Guadalajara', 'JAL', '44100', 'Mexico'), 179 | (24, 'Victor', 'Adams', 'victora@hotmail.com', '555-4567', '1212 Birch Blvd', 'Monterrey', 'NLE', '64000', 'Mexico'), 180 | (25, 'Walter', 'Baker', 'walterb@live.com', '555-5678', '1313 Oak Blvd', 'New York', 'NY', '10001', 'USA'), 181 | (26, 'Xander', 'Nelson', 'xandern@gmail.com', '555-6789', '1414 Cedar Blvd', 'Los Angeles', 'CA', '90001', 'USA'), 182 | (27, 'Yvonne', 'Carter', 'yvonnec@yahoo.com', '555-7890', '1515 Maple Dr', 'Chicago', 'IL', '60601', 'USA'), 183 | (28, 'Zane', 'Mitchell', 'zanem@outlook.com', '555-8901', '1616 Redwood Dr', 'Houston', 'TX', '77001', 'USA'), 184 | (29, 'Anna', 'Roberts', 'annar@hotmail.com', '555-9012', '1717 Willow Dr', 'Sydney', 'NSW', '2000', 'Australia'), 185 | (30, 'Ben', 'King', 'benk@live.com', '555-0123', '1818 Birch Dr', 'Melbourne', 'VIC', '3000', 'Australia'), 186 | (31, 'Chloe', 'Green', 'chloeg@gmail.com', '555-1234', '1919 Oak Dr', 'Toronto', 'ON', 'M4B1B3', 'Canada'), 187 | (32, 'Daniel', 'Evans', 'daniele@yahoo.com', '555-2345', '2020 Cedar Dr', 'Vancouver', 'BC', 'V5K0A1', 'Canada'), 188 | (33, 'Ella', 'Collins', 'ellac@outlook.com', '555-3456', '2121 Redwood Dr', 'London', 'England', 'SW1A 1AA', 'UK'), 189 | (34, 'Finn', 'Morris', 'finnm@hotmail.com', '555-4567', '2222 Willow St', 'Manchester', 'England', 'M1 1AE', 'UK'), 190 | (35, 'Grace', 'Lee', 'gracel@live.com', '555-5678', '2323 Birch St', 'Berlin', 'BE', '10117', 'Germany'), 191 | (36, 'Holly', 'Martinez', 'hollym@gmail.com', '555-6789', '2424 Oak St', 'Munich', 'BY', '80331', 'Germany'), 192 | (37, 'Ian', 'Robinson', 'ianr@yahoo.com', '555-7890', '2525 Cedar St', 'Warsaw', 'WA', '00-001', 'Poland'), 193 | (38, 'Jasmine', 'Walker', 'jasminew@outlook.com', '555-8901', '2626 Redwood St', 'Lisbon', 'LI', '1100-148', 'Portugal'), 194 | (39, 'Kyle', 'Young', 'kyley@hotmail.com', '555-9012', '2727 Willow St', 'Pittsburgh', 'PA', '15201','USA'), 195 | (40, 'Liam', 'Harris', 'liamh@live.com', '555-0123', '2828 Birch St', 'Richmond', 'VA', '23220','USA'); 196 | 197 | DROP TABLE IF EXISTS Payments; 198 | DROP TABLE IF EXISTS Orders; 199 | 200 | CREATE TABLE Orders ( 201 | OrderID INT PRIMARY KEY, 202 | CustomerID INT, 203 | ProductID INT NULL, 204 | OrderDate DATE, 205 | Quantity INT, 206 | TotalAmount DECIMAL(10, 2) 207 | ); 208 | 209 | -- Insert 40 rows into Orders with randomized IDs and NULL ProductIDs for some 210 | INSERT INTO Orders VALUES 211 | (102, 15, 2, '2023-05-14', 1, 800.00), 212 | (205, 8, 3, '2024-09-07', 2, 800.00), 213 | (311, 22, 4, '2022-11-22', 1, 250.00), 214 | (421, 33, 5, '2021-03-30', 3, 150.00), 215 | (523, 40, NULL, '2025-07-19', 1, 30.00), 216 | (610, 19, 7, '2022-08-25', 2, 300.00), 217 | (728, 14, 8, '2024-06-10', 1, 200.00), 218 | (812, 27, 9, '2021-12-04', 4, 40.00), 219 | (915, 6, 10, '2023-02-18', 1, 10.00), 220 | (1003, 31, NULL, '2025-09-27', 2, 360.00), 221 | (1108, 12, 12, '2023-10-11', 1, 500.00), 222 | (1205, 29, 13, '2024-04-03', 1, 25.00), 223 | (1354, 4, 14, '2022-07-29', 2, 60.00), 224 | (1411, 9, 15, '2021-01-22', 3, 135.00), 225 | (1533, 23, NULL, '2025-11-15', 1, 80.00), 226 | (1622, 36, 17, '2022-10-08', 1, 60.00), 227 | (1710, 1, 18, '2023-06-21', 2, 40.00), 228 | (1845, 20, 19, '2021-09-13', 5, 50.00), 229 | (1908, 37, 20, '2025-03-05', 2, 50.00), 230 | (2042, 10, 21, '2024-08-14', 1, 60.00), 231 | (2157, 13, 22, '2022-12-01', 1, 100.00), 232 | (2268, 35, 23, '2023-09-09', 1, 15.00), 233 | (2349, 7, 24, '2021-07-18', 2, 180.00), 234 | (2481, 17, NULL, '2025-06-23', 3, 15.00), 235 | (2594, 5, 26, '2023-03-12', 4, 100.00), 236 | (2633, 26, 27, '2022-04-07', 1, 450.00), 237 | (2782, 39, 28, '2024-11-30', 1, 600.00), 238 | (2845, 24, 29, '2021-02-25', 1, 500.00), 239 | (2903, 2, 30, '2025-05-28', 2, 240.00), 240 | (3011, 16, 31, '2023-08-20', 1, 350.00), 241 | (3152, 21, 32, '2022-01-17', 1, 450.00), 242 | (3227, 38, 33, '2025-09-10', 1, 40.00), 243 | (3368, 11, 34, '2021-04-04', 2, 100.00), 244 | (3419, 3, 35, '2024-07-15', 3, 120.00), 245 | (3551, 18, 36, '2022-10-31', 1, 60.00), 246 | (3684, 32, 37, '2023-12-22', 1, 35.00), 247 | (3749, 28, 38, '2021-06-06', 2, 110.00), 248 | (3872, 25, NULL, '2025-02-01', 1, 40.00), 249 | (3911, 30, 40, '2023-11-26', 3, 120.00), 250 | (4057, 22, 1, '2024-03-09', 1, 1200.00); 251 | 252 | DROP TABLE IF EXISTS Payments; 253 | 254 | CREATE TABLE Payments ( 255 | PaymentID INT PRIMARY KEY, 256 | OrderID INT NULL, 257 | PaymentDate DATE, 258 | Amount DECIMAL(10, 2), 259 | PaymentMethod VARCHAR(50), 260 | FOREIGN KEY (OrderID) REFERENCES Orders(OrderID) 261 | ); 262 | 263 | -- Insert 30 payments, some without matching orders 264 | INSERT INTO Payments VALUES 265 | (501, 102, '2023-05-15', 800.00, 'Credit Card'), 266 | (502, 205, '2024-09-08', 800.00, 'PayPal'), 267 | (503, 311, '2022-11-23', 250.00, 'Bank Transfer'), 268 | (504, NULL, '2024-01-05', 500.00, 'Cash'), 269 | (505, 421, '2021-03-31', 150.00, 'Credit Card'), 270 | (506, 610, '2022-08-26', 300.00, 'Debit Card'), 271 | (507, 728, '2024-06-11', 200.00, 'PayPal'), 272 | (508, NULL, '2023-12-20', 75.00, 'Bank Transfer'), 273 | (509, 915, '2023-02-19', 10.00, 'Cash'), 274 | (510, 1003, '2025-09-28', 360.00, 'Credit Card'), 275 | (511, 1108, '2023-10-12', 500.00, 'PayPal'), 276 | (512, 1205, '2024-04-04', 25.00, 'Debit Card'), 277 | (513, 1354, '2022-07-30', 60.00, 'Credit Card'), 278 | (514, NULL, '2024-02-17', 1200.00, 'Wire Transfer'), 279 | (515, 1411, '2021-01-23', 135.00, 'Bank Transfer'), 280 | (516, NULL, '2023-06-29', 150.00, 'PayPal'), 281 | (517, 1622, '2022-10-09', 60.00, 'Cash'), 282 | (518, 1710, '2023-06-22', 40.00, 'Debit Card'), 283 | (519, 1845, '2021-09-14', 50.00, 'Credit Card'), 284 | (520, NULL, '2025-01-18', 95.00, 'Bank Transfer'); 285 | 286 | DROP TABLE IF EXISTS Products; 287 | DROP TABLE IF EXISTS Suppliers; 288 | -- Create Suppliers Table 289 | CREATE TABLE Suppliers ( 290 | SupplierID INT PRIMARY KEY, 291 | SupplierName VARCHAR(100), 292 | ContactName VARCHAR(100), 293 | Email VARCHAR(150) 294 | ); 295 | 296 | INSERT INTO Suppliers (SupplierID, SupplierName, ContactName, Email) VALUES 297 | (1, 'Tech Distributors', 'Alice Johnson', 'alice@techdist.com'), 298 | (2, 'Gadget Supplies', 'Bob Smith', 'bob@gadgetsupplies.com'), 299 | (3, 'Office Essentials', 'Charlie Davis', 'charlie@officeessentials.com'), 300 | (4, 'Furniture Hub', 'Diana Adams', 'diana@furniturehub.com'), 301 | (5, 'Clothing Mart', 'Ethan Clark', 'ethan@clothingmart.com'), 302 | (6, 'Car Parts', 'Tom Hanks', 'tom@carparts.com'), 303 | (7, 'Toys', 'John Adams', 'john@toysff.com'); 304 | 305 | CREATE TABLE Products ( 306 | ProductID INT PRIMARY KEY, 307 | ProductName VARCHAR(100), 308 | Price DECIMAL(10, 2), 309 | Category VARCHAR(50), 310 | StockQuantity INT, 311 | SupplierID INT, 312 | FOREIGN KEY (SupplierID) REFERENCES Suppliers(SupplierID) 313 | ); 314 | 315 | INSERT INTO Products (ProductID, ProductName, Price, Category, StockQuantity, SupplierID) VALUES 316 | (1, 'Laptop', 1200.00, 'Electronics', 30, 1), 317 | (2, 'Smartphone', 800.00, 'Electronics', 50, 1), 318 | (3, 'Tablet', 400.00, 'Electronics', 40, 1), 319 | (4, 'Monitor', 250.00, 'Electronics', 60, 1), 320 | (5, 'Keyboard', 50.00, 'Accessories', 100, 2), 321 | (6, 'Mouse', 30.00, 'Accessories', 120, 2), 322 | (7, 'Chair', 150.00, 'Furniture', 80, 4), 323 | (8, 'Desk', 200.00, 'Furniture', 75, 4), 324 | (9, 'Pen', 5.00, 'Stationery', 300, 3), 325 | (10, 'Notebook', 10.00, 'Stationery', 500, 3), 326 | (11, 'Printer', 180.00, 'Electronics', 25, 1), 327 | (12, 'Camera', 500.00, 'Electronics', 40, 1), 328 | (13, 'Flashlight', 25.00, 'Tools', 200, 2), 329 | (14, 'Shirt', 30.00, 'Clothing', 150, 5), 330 | (15, 'Jeans', 45.00, 'Clothing', 120, 5), 331 | (16, 'Jacket', 80.00, 'Clothing', 70, 5), 332 | (17, 'Shoes', 60.00, 'Clothing', 100, 5), 333 | (18, 'Hat', 20.00, 'Accessories', 50, 2), 334 | (19, 'Socks', 10.00, 'Clothing', 200, 5), 335 | (20, 'T-Shirt', 25.00, 'Clothing', 150, 5), 336 | (21, 'Lamp', 60.00, 'Furniture', 40, 4), 337 | (22, 'Coffee Table', 100.00, 'Furniture', 35, 4), 338 | (23, 'Book', 15.00, 'Stationery', 250, 3), 339 | (24, 'Rug', 90.00, 'Furniture', 60, 4), 340 | (25, 'Cup', 5.00, 'Accessories', 500, 2), 341 | (26, 'Bag', 25.00, 'Accessories', 300, 2), 342 | (27, 'Couch', 450.00, 'Furniture', 15, 4), 343 | (28, 'Fridge', 600.00, 'Electronics', 20, 1), 344 | (29, 'Stove', 500.00, 'Electronics', 15, 1), 345 | (30, 'Microwave', 120.00, 'Electronics', 25, 1), 346 | (31, 'Air Conditioner', 350.00, 'Electronics', 10, 1), 347 | (32, 'Washing Machine', 450.00, 'Electronics', 15, 1), 348 | (33, 'Dryer', 400.00, 'Electronics', 10, 1), 349 | (34, 'Hair Dryer', 30.00, 'Accessories', 100, 2), 350 | (35, 'Iron', 40.00, 'Electronics', 50, 1), 351 | (36, 'Coffee Maker', 50.00, 'Electronics', 60, 1), 352 | (37, 'Blender', 35.00, 'Electronics', 40, 1), 353 | (38, 'Juicer', 55.00, 'Electronics', 30, 1), 354 | (39, 'Toaster', 40.00, 'Electronics', 70, 1), 355 | (40, 'Dishwasher', 500.00, 'Electronics', 20, 1); 356 | 357 | DROP TABLE IF EXISTS Categories; 358 | 359 | -- Create Categories Table 360 | CREATE TABLE Categories ( 361 | CategoryID INT PRIMARY KEY IDENTITY, 362 | CategoryName VARCHAR(50) UNIQUE 363 | ); 364 | 365 | -- Insert Unique Categories 366 | INSERT INTO Categories (CategoryName) VALUES 367 | ('Electronics'), 368 | ('Accessories'), 369 | ('Furniture'), 370 | ('Stationery'), 371 | ('Tools'), 372 | ('Clothing'); 373 | 374 | UPDATE P 375 | SET P.Category = C.CategoryID 376 | FROM Products P JOIN Categories C ON P.Category=C.CategoryName 377 | 378 | UPDATE Products 379 | SET Category = 10 380 | WHERE Category = 4 381 | 382 | ALTER TABLE Products 383 | ALTER COLUMN Category INT NULL 384 | 385 | DROP TABLE IF EXISTS Enrollments; 386 | DROP TABLE IF EXISTS Students; 387 | DROP TABLE IF EXISTS Courses; 388 | 389 | CREATE TABLE Students ( 390 | StudentID INT PRIMARY KEY, 391 | Name VARCHAR(100), 392 | Age INT, 393 | Major VARCHAR(50) 394 | ); 395 | 396 | CREATE TABLE Courses ( 397 | CourseID INT PRIMARY KEY, 398 | CourseName VARCHAR(100), 399 | Instructor VARCHAR(100) 400 | ); 401 | 402 | CREATE TABLE Enrollments ( 403 | EnrollmentID INT PRIMARY KEY IDENTITY, 404 | StudentID INT, 405 | CourseID INT, 406 | FOREIGN KEY (StudentID) REFERENCES Students(StudentID), 407 | FOREIGN KEY (CourseID) REFERENCES Courses(CourseID) 408 | ); 409 | 410 | INSERT INTO Students (StudentID, Name, Age, Major) VALUES 411 | (1, 'Alice Johnson', 20, 'Computer Science'), 412 | (2, 'Bob Smith', 21, 'Mathematics'), 413 | (3, 'Charlie Brown', 22, 'Physics'), 414 | (4, 'David Wilson', 19, 'Engineering'), 415 | (5, 'Emma Davis', 20, 'Biology'), 416 | (6, 'Frank Moore', 23, 'Business'), 417 | (7, 'Grace Taylor', 21, 'Mathematics'), 418 | (8, 'Henry Anderson', 20, 'Economics'), 419 | (9, 'Isabella Thomas', 22, 'Chemistry'), 420 | (10, 'Jack White', 19, 'Computer Science'), 421 | (11, 'Karen Harris', 20, 'Mathematics'), 422 | (12, 'Liam Martin', 21, 'Engineering'), 423 | (13, 'Mia Clark', 22, 'Biology'), 424 | (14, 'Noah Lewis', 20, 'Physics'), 425 | (15, 'Olivia Walker', 19, 'Business'), 426 | (16, 'Paul Hall', 22, 'Computer Science'), 427 | (17, 'Quinn Young', 21, 'Economics'), 428 | (18, 'Rachel King', 20, 'Chemistry'), 429 | (19, 'Samuel Allen', 23, 'Engineering'), 430 | (20, 'Taylor Scott', 21, 'Mathematics'), 431 | (21, 'Ursula Green', 20, 'Biology'), 432 | (22, 'Victor Adams', 22, 'Physics'), 433 | (23, 'Wendy Baker', 19, 'Business'), 434 | (24, 'Xavier Cooper', 20, 'Computer Science'), 435 | (25, 'Yvonne Nelson', 21, 'Mathematics'), 436 | (26, 'Zachary Carter', 22, 'Engineering'), 437 | (27, 'Amelia Wright', 20, 'Biology'), 438 | (28, 'Benjamin Mitchell', 19, 'Physics'), 439 | (29, 'Charlotte Perez', 22, 'Business'), 440 | (30, 'Daniel Roberts', 21, 'Economics'), 441 | (31, 'Eleanor Stewart', 20, 'Chemistry'), 442 | (32, 'Frederick Reed', 23, 'Engineering'), 443 | (33, 'Gabriella Howard', 21, 'Mathematics'), 444 | (34, 'Harrison Bell', 20, 'Biology'), 445 | (35, 'Ivy Collins', 22, 'Physics'), 446 | (36, 'Jason Murphy', 19, 'Business'), 447 | (37, 'Kylie Foster', 21, 'Computer Science'), 448 | (38, 'Lucas Jenkins', 20, 'Economics'), 449 | (39, 'Madeline Perry', 22, 'Chemistry'), 450 | (40, 'Nathan Griffin', 19, 'Engineering'); 451 | 452 | INSERT INTO Courses (CourseID, CourseName, Instructor) VALUES 453 | (1, 'Math 101', 'Dr. Adams'), 454 | (2, 'Physics 101', 'Dr. Brown'), 455 | (3, 'Biology 101', 'Dr. Carter'), 456 | (4, 'Computer Science 101', 'Dr. Davis'), 457 | (5, 'Business 101', 'Dr. Evans'); 458 | 459 | INSERT INTO Enrollments (StudentID, CourseID) VALUES 460 | (1, 1), (2, 1), (7, 1), (11, 1), (20, 1), (25, 1), (33, 1), 461 | (3, 2), (9, 2), (14, 2), (22, 2), (28, 2), (35, 2), 462 | (5, 3), (13, 3), (21, 3), (27, 3), (34, 3), 463 | (10, 4), (16, 4), (24, 4), (30, 4), (37, 4), 464 | (6, 5), (15, 5), (23, 5), (29, 5), (36, 5); 465 | 466 | DROP TABLE IF EXISTS Sales; 467 | 468 | CREATE TABLE Sales ( 469 | SaleID INT PRIMARY KEY, 470 | ProductID INT, 471 | CustomerID INT, 472 | SaleDate DATE, 473 | SaleAmount DECIMAL(10, 2) 474 | ); 475 | 476 | INSERT INTO Sales (SaleID, ProductID, CustomerID, SaleDate, SaleAmount) VALUES 477 | (1, 1, 1, '2023-01-01', 150.00), 478 | (2, 2, 2, '2023-01-02', 200.00), 479 | (3, 3, 3, '2023-01-03', 250.00), 480 | (4, 4, 4, '2023-01-04', 300.00), 481 | (5, 5, 5, '2023-01-05', 350.00), 482 | (6, 6, 6, '2023-01-06', 400.00), 483 | (7, 7, 7, '2023-01-07', 450.00), 484 | (8, 8, 8, '2023-01-08', 500.00), 485 | (9, 9, 9, '2023-01-09', 550.00), 486 | (10, 10, 10, '2023-01-10', 600.00), 487 | (11, 1, 1, '2023-01-11', 150.00), 488 | (12, 2, 2, '2023-01-12', 200.00), 489 | (13, 3, 3, '2023-01-13', 250.00), 490 | (14, 4, 4, '2023-01-14', 300.00), 491 | (15, 5, 5, '2023-01-15', 350.00), 492 | (16, 6, 6, '2023-01-16', 400.00), 493 | (17, 7, 7, '2023-01-17', 450.00), 494 | (18, 8, 8, '2023-01-18', 500.00), 495 | (19, 9, 9, '2023-01-19', 550.00), 496 | (20, 10, 10, '2023-01-20', 600.00), 497 | (21, 1, 2, '2023-01-21', 150.00), 498 | (22, 2, 3, '2023-01-22', 200.00), 499 | (23, 3, 4, '2023-01-23', 250.00), 500 | (24, 4, 5, '2023-01-24', 300.00), 501 | (25, 5, 6, '2023-01-25', 350.00), 502 | (26, 6, 7, '2023-01-26', 400.00), 503 | (27, 7, 8, '2023-01-27', 450.00), 504 | (28, 8, 9, '2023-01-28', 500.00), 505 | (29, 9, 10, '2023-01-29', 550.00), 506 | (30, 10, 1, '2023-01-30', 600.00), 507 | (31, 1, 2, '2023-02-01', 150.00), 508 | (32, 2, 3, '2023-02-02', 200.00), 509 | (33, 3, 4, '2023-02-03', 250.00), 510 | (34, 4, 5, '2023-02-04', 300.00), 511 | (35, 5, 6, '2023-02-05', 350.00), 512 | (36, 6, 7, '2023-02-06', 400.00), 513 | (37, 7, 8, '2023-02-07', 450.00), 514 | (38, 8, 9, '2023-02-08', 500.00), 515 | (39, 9, 10, '2023-02-09', 550.00), 516 | (40, 10, 1, '2023-02-10', 600.00); 517 | ``` 518 | --------------------------------------------------------------------------------