The document discusses SQL aggregation and grouping, highlighting functions like SUM, AVG, and COUNT, and how to use GROUP BY and HAVING clauses for data summarization. It also covers SQL joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, and SELF JOIN, explaining their use cases and how they relate tables. Additionally, it mentions UNION and UNION ALL for combining results from multiple SELECT queries.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
Reviewer SQL Lab2
The document discusses SQL aggregation and grouping, highlighting functions like SUM, AVG, and COUNT, and how to use GROUP BY and HAVING clauses for data summarization. It also covers SQL joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, and SELF JOIN, explaining their use cases and how they relate tables. Additionally, it mentions UNION and UNION ALL for combining results from multiple SELECT queries.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Reviewer: Selecting Data, Example Queries & Expected
Aggregation, and Grouping in Outputs
SQL 1. Total Salary per Department – Uses `SUM(salary)` with `GROUP Aggregation and Grouping in BY department`. SQL 2. Average Salary per Job Title – - Aggregation allows summarizing Uses `AVG(salary)` with `GROUP data using functions like `SUM`, BY job_title`. `COUNT`, `AVG`, etc. 3. Min & Max Salary per - `GROUP BY` organizes data into Department – Uses `MIN(salary)`, categories. `MAX(salary)`. - `HAVING` filters grouped data 4. Grouping by Year of Hire – Uses based on conditions. `YEAR(hire_date)` with `GROUP Common Aggregate Functions BY`. 1. `SUM(column)` – Returns the total sum of values. Reviewer: Selecting Data & 2. `COUNT(column)` – Returns Joining Tables in SQL the number of rows (ignores Introduction to SQL Joins `NULL`). - Joining tables in SQL allows 3. `AVG(column)` – Returns retrieving data from multiple the average value. related tables using a common 4. `MIN(column)` – Returns field. the smallest value. - Relational databases use foreign 5. `MAX(column)` – Returns keys to establish relationships the largest value. between tables. Using the GROUP BY Clause Common Types of Joins in SQL - Groups rows that share the same 1. INNER JOIN (Default Join) values. - Returns only the rows where - Used with aggregate functions to there is a match between both organize data meaningfully. tables. - Example: Counting employees - Rows without matching records per department. are excluded. Filtering Groups with HAVING - Use Case: When you need data - `HAVING` filters results after that exists in both tables (e.g., `GROUP BY`. employees belonging to a - Used with aggregate functions department). (unlike `WHERE`, which filters 2. LEFT JOIN (LEFT OUTER before grouping). JOIN) - Example: Finding departments - Returns all records from the where total salary exceeds left table and matching records 170,000. from the right table. Combining WHERE, GROUP - If no match is found, `NULL` BY, and HAVING values are returned for the right - `WHERE` filters before grouping. table. - `GROUP BY` organizes data. - Use Case: To see all - `HAVING` filters after employees, including those grouping. without a department. - Use Case: Merging datasets from multiple sources (e.g., combining retail and online customer lists). 3. RIGHT JOIN (RIGHT OUTER Examples & Expected Outputs JOIN) 1. Employees per Department - Returns all records from the using INNER JOIN. right table and matching records 2. All Employees (even without a from the left table. department) using LEFT JOIN. - If no match exists, `NULL` 3. All Departments (even without values appear for the left table. employees) using RIGHT JOIN. - Use Case: To see all 4. Merging two customer lists departments, including those using UNION and UNION ALL. without employees. 4. FULL JOIN (FULL OUTER JOIN) - Combines results of LEFT JOIN and RIGHT JOIN. - Returns all records from both tables, with `NULL` values where no match is found. - Use Case: When a complete dataset, including unmatched records from both tables, is needed. 5. CROSS JOIN - Creates a Cartesian product (every row from the first table joins with every row from the second table). - Use Case: To generate all possible combinations (e.g., pairing all employees with all departments for testing). 6. SELF JOIN - A table joins with itself, useful for hierarchical relationships (e.g., employees and their managers). - Use Case: Working with hierarchical data like reporting structures (e.g., employees reporting to managers). UNION and UNION ALL - Used to combine results of multiple `SELECT` queries into one result set. - UNION removes duplicates, while UNION ALL retains all records.