Advanced SQL Queries

Master advanced SQL queries like subqueries, joins, views, and more to manage and analyze complex data.

Main Tables: Employees, Products, Customers, and Orders

Here are the main tables used for many of the queries below:

Employee ID Name Salary Department
1 John Doe $80,000 IT
2 Jane Smith $60,000 HR
Product ID Product Name Price
201 Laptop $1,200
202 Smartphone $800
Customer ID Name City
301 John Doe New York
302 Jane Smith Los Angeles
Order ID Customer ID Order Total
401 301 $1,200
402 302 $800

1. Subqueries

A subquery is a query nested inside another query. It's used to fetch results that will be used by the main query. Example:

SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

This query retrieves the names of employees whose salary is greater than the average salary.

Name
John Doe

Additional Subquery Example:

SELECT product_name FROM products WHERE price > (SELECT AVG(price) FROM products);

This query retrieves all products that are priced above the average product price.

Product Name
Laptop

2. JOINS

Advanced SQL queries often involve joining multiple tables to retrieve meaningful results. Example:

SELECT customers.name, orders.order_id FROM customers 
JOIN orders ON customers.customer_id = orders.customer_id;

This query retrieves customer names along with their respective order IDs by joining the `customers` and `orders` tables.

Customer Name Order ID
John Doe 401
Jane Smith 402

Additional Join Example:

SELECT employees.name, departments.department_name FROM employees 
JOIN departments ON employees.department_id = departments.department_id;

This query retrieves employee names along with their respective department names by joining the `employees` and `departments` tables.

3. Views

A SQL view is a virtual table based on the result-set of a query. Example:

CREATE VIEW employee_salaries AS 
SELECT name, salary FROM employees;

This query creates a view that shows employee names and their respective salaries.

Using the View Example:

SELECT * FROM employee_salaries;

This query retrieves data from the `employee_salaries` view.

Name Salary
John Doe $80,000
Jane Smith $60,000

4. Indexes

Indexes are used to speed up the retrieval of rows by using a pointer. Example:

CREATE INDEX idx_customer_name ON customers (name);

This query creates an index on the `name` column of the `customers` table to speed up queries that filter or sort by name.

Using Index in a Query Example:

SELECT * FROM customers WHERE name = 'John Doe';

This query retrieves all records where the customer’s name is 'John Doe' using the created index for faster lookup.

Customer ID Name City
301 John Doe New York

5. Aggregate Functions

SQL provides several aggregate functions to perform calculations on sets of values. Example using `COUNT`:

SELECT COUNT(*) FROM orders WHERE order_total > 1000;

This query counts the number of orders with an order total greater than $1000.

Count of Orders
1

Conclusion

Mastering advanced SQL queries enables efficient data manipulation and analysis, essential for handling complex databases and drawing meaningful insights.