Sql notes
Sql notes
Syntax
FROM table1
Types of Subqueries
4. Correlated Subquery: References columns from the outer query and executes repeatedly for
each row of the outer query.
Examples
SELECT employee_name
FROM employees
FROM employees
FROM employees;
4. Correlated Subquery
Find employees whose salaries are greater than the average salary in their department.
SELECT employee_name
FROM employees e1
FROM employees e2
Key Considerations
1. Performance: Subqueries can be slower than joins or Common Table Expressions (CTEs) for
large datasets.
2. Nesting Levels: Too many nested subqueries can make queries harder to read and maintain.
SQL offers a wide range of functions that are extremely useful for data analysis. These functions can
be categorized into aggregate functions, string functions, date/time functions, and window
functions. Here's a breakdown:
1. Aggregate Functions
SUM() Calculates the total sum of a column. SELECT SUM(sales) FROM orders;
2. String Functions
YEAR(), MONTH(), DAY() Extracts components of a date. SELECT YEAR(order_date) FROM orders;
4. Mathematical Functions
ROUND() Rounds a number to a specified number of digits. SELECT ROUND(price, 2) FROM products;
5. Window Functions
Perform calculations across a set of table rows related to the current row.
COde
SELECT department,
COUNT(employee_id) AS employee_count,
AVG(salary) AS avg_salary,
MAX(hire_date) AS latest_hire
FROM employees
GROUP BY department