Загрузка...

Master SQL Subqueries in 3 Minutes! (Full Guide)

For some, the video may be a little silent. You may want to turn your volume up.

Imagine we have a table in our MySQL database management system related to employees.
In that case, we may have an employee, a department, and a salary of an employee as columns.

Sometimes, we may need to calculate what are the employees that make more than an average salary, and for that, we may use an SQL query like so:
SELECT employee, salary FROM employees WHERE salary [more than] (SELECT AVG(salary) FROM employees);

This SQL query would be called a SQL subquery because it contains a query within itself.
This SQL subquery would also be called a scalar subquery because it's acting on a subquery that returns one value (an average salary from the employees table in a relational database.)

SQL subqueries can also be correlated. Imagine that we have a SQL subquery like so:
SELECT employee, salary, department FROM employees e1
WHERE salary [more than] (
SELECT AVG(salary)
FROM employees e2
WHERE e1.department = e2.department
);

Such an SQL subquery would be called correlated because it's an SQL query that references columns from the outer SQL query.
Look closely: our original SQL query references the department database column which is also referenced by our SQL subquery.

Scalar SQL subqueries act on one value, while correlated SQL subqueries depend on values from another (outer) SQL query.

Enjoyed this SQL subqueries tutorial? make sure to share it with your friends!

Music:
Balearic Love by tubebackr: https://soundcloud.com/tubebackr
License: Creative Commons. Attribution-NoDerivs 3.0 Unported. CC BY-ND 3.0
Free Download / Stream: https://www.audiolibrary.com.co/tubebackr/balearic-love
Music promoted by Audio Library: https://www.youtube.com/watch?v=-kEFWcvXsZA

#database #sql #programming #development #developer

Видео Master SQL Subqueries in 3 Minutes! (Full Guide) канала Database Dive
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять