SQL is an efficient language that is used to manage and analyze database data contained in relational databases. Joins and window functions are considered to be among the most significant tools of high-level data querying.
SQL Joins - Fusion of Different Tables.
The SQL used to put together rows of two or more tables is a JOIN in SQL. It is done by joining 2 or more tables using a common column in both tables being joined. Joins are important to work with normalized databases, in which data is distributed across a number of tables to minimize redundancy.
Types of Joins
INNER JOIN
Returns only the rows that have matching values in both tables.
Sample: Selecting the names of the employees and the name of the department in which they work. There will only be the employees in a department; employees who have no department or departments that have no employees will not be returned.
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;
LEFT JOIN (or LEFT OUTER JOIN)
Gives back all the rows of the left table and the matching rows from the right table. In case no match is found, NULLs are shown.
Sample: Giving a list of all the employees, including those who are not assigned to any department.
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id;
RIGHT JOIN (or right outer join)
Returns all rows from the right table and the matching rows from the left table.
Example: Selecting all departments, even those that have no workers/employees.
SELECT e.name, d.department_name
FROM departments d
RIGHT JOIN employees e
ON e.department_id = d.department_id;
FULL JOIN (full outer join)
Returns everything from the joined tables.
Example: Combining the employees and department tables.
SELECT e.name, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;
CROSS JOIN
Gets the Cartesian product of two tables. Each row of the first table is multiplied by each row of the second one. Essentially, if table one has 5 rows and row 2 has 4 rows, you would expect a total of 20 rows in the cross-joined table.
Example: Generating the combinations of employees and departments.
SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;
One thing about cross joins, it doesn't need the on statement.
Window Functions: Row-Level Calculations
Window functions allow you to perform calculations across a set of rows that are related to the current row.
Common Window Functions
ROW_NUMBER()
Assigns a unique sequential integer to rows within a partition.
Example: Ranking employees by salary within each department.
SELECT e.name, d.department_id, department_name, e.salary, ROW_NUMBER() OVER (PARTITION BY department_name ORDER BY salary) AS salary_rank
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;
RANK() and DENSE_RANK()
RANK gives a sequence to rows, but in the case of rows with a similar value, it skips ranks based on ties, while DENSE_RANK doesn't skip ranks even in a tie.
SELECT name, department_id, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees;
SUM(), AVG, MIN, and MAX as Window Functions
Carries out aggregate inspections on a window of rows.
Example: Calculating the total salary expense per department while still showing individual employees.
SELECT name, department_id, salary, SUM(salary) OVER (PARTITION BY department_id) AS total_salary
FROM employees;
LEAD() and LAG()
Accesses subsequent or previous row values within a partition.
Example: Comparing the salary of each of the employees with both the past and the future salary in the dataset.
SELECT name, salary,
LAG(salary, 1) OVER (ORDER BY salary) AS previous_salary,
LEAD(salary, 1) OVER (ORDER BY salary) AS next_salary
FROM employees;
TIP
Figuring out how to rank, do totals, calculate moving averages, and make comparisons between current and previous values, window functions are best suited for analytics efforts where row-level information cannot be lost.
The Joins and Window Functions Combination.
Joins and window functions tend to be used together when it comes to more intricate analytics. For instance, you could join employees with departments, then rank salaries within each department:
SELECT e.name, d.department_name, e.salary, RANK() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) AS salary_rank
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
This query provides a ranked view of salaries for each department, combining relational data retrieval with advanced row-wise computation.
Conclusion
Joins and window functions are highly applicable in SQL. Joins make it easy and effective to combine data from several tables. Windows functions allow one to compute calculations of rows without losing any detail. A combination of the two will provide more powerful insights on your datasets. They are useful in ranking, totals, comparisons, and analytics. The practice of these functions will make your SQL queries strong. Working with data involves working with learning joins as well as window functions, which is a crucial part of the work of any data professional.
Top comments (0)