This document outlines the learning objectives and key concepts of advanced SQL queries, including various types of joins, subqueries, aggregate functions, and query optimization techniques. It provides examples of SQL queries for INNER JOIN and subqueries, as well as aggregate functions like COUNT, SUM, and AVG. Additionally, it includes student exercises to practice these concepts.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views
Advanced_SQL_Queries
This document outlines the learning objectives and key concepts of advanced SQL queries, including various types of joins, subqueries, aggregate functions, and query optimization techniques. It provides examples of SQL queries for INNER JOIN and subqueries, as well as aggregate functions like COUNT, SUM, and AVG. Additionally, it includes student exercises to practice these concepts.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Module 3: Advanced SQL Queries
Advanced SQL Queries and
Optimization Yazz Communication Academy Learning Objectives • - Understand and write complex SQL queries • - Use different types of joins to retrieve data from multiple tables • - Apply subqueries to extract data efficiently • - Utilize aggregate functions for data analysis • - Optimize queries to improve performance SQL Joins • - INNER JOIN: Returns only matching rows from both tables • - LEFT JOIN: Returns all rows from the left table and matching rows from the right table • - RIGHT JOIN: Returns all rows from the right table and matching rows from the left table • - FULL OUTER JOIN: Returns all rows when there is a match in either table • - SELF JOIN: Joins a table with itself • - CROSS JOIN: Returns the Cartesian product Example: INNER JOIN • SQL Query: • SELECT employees.name, departments.department_name • FROM employees • INNER JOIN departments ON employees.department_id = departments.id; Subqueries • - Nested queries: A query inside another query • - Correlated subqueries: A subquery that references the outer query • - Using subqueries in SELECT, FROM, and WHERE clauses Example: Subquery • SQL Query: • SELECT name, salary • FROM employees • WHERE salary > (SELECT AVG(salary) FROM employees); Aggregate Functions • - COUNT(): Counts the number of rows • - SUM(): Calculates the sum of a column • - AVG(): Calculates the average value • - MIN() / MAX(): Returns the smallest or largest value • - GROUP BY: Groups data by a column • - HAVING: Filters grouped results Query Optimization Techniques • - Using indexes to speed up searches • - Avoiding unnecessary columns in SELECT queries • - Using EXPLAIN to analyze query performance Student Exercises • 1. Write a query using LEFT JOIN to retrieve all employees and their department names, including those without a department. • 2. Use a subquery to find courses with more than 20 students enrolled. • 3. Apply SUM and GROUP BY to calculate the total salary paid per department. • 4. Optimize an existing query by adding an index to a frequently searched column.