We Learn SQL #15 | SQL Subqueries in SELECT

preview_player
Показать описание
We’re going to learn about SQL subqueries in the SELECT clause. Find out how to use them correctly and how it will improve your work with databases.

It is very important to remember that a subquery in the SELECT clause must return exactly one row with exactly one column. If it returns more than that, you’ll get an error, and the query won’t work. For this reason, this sort of subquery frequently uses an aggregate function such as AVERAGE or SUM because such a function aggregates the rows and returns exactly one value.

Without further ado, let’s get started!

👌 Subscribe to our channel and leave a comment!

👌 Do you want to learn SQL? Go to LearnSQL.com and choose the course or track that is best for you!

Рекомендации по теме
Комментарии
Автор

Great video! I just want to add that subqueries in the select statement are also referred to as 'subselects' and that a subselect query can reference columns in the select query. However, subqueries used in join cannot. Queries used in joins can also have multiple columns and are similar to a common table expressions (CTE). In SQL the only parts you need single-row unique columns are in: subselects and in where clause subqueries.

davidcorona
Автор

Please lower down the volume of background music

Rafian
Автор

Thanks. Never learn this method before.

lawngreenlyp
Автор

Thanks for the tutorial. Very helpful.

simplemente_humano
Автор

What if we need column name, instead of aggregates ?

Priyankayadav-ztck
Автор

The background music is very distracting. Unfortunately, it is difficult to hear and listen to the presented learning content. Please consider lowering the music by 90% or simply eliminate it. Thanks!

uc
Автор

DECLARE @eighteen_months_ago DATE;
SET @eighteen_months_ago = DATEADD(MONTH, -18, GETDATE());

-- Convert the date to a VARCHAR in the format 'yyyy-MM-dd'
DECLARE @date_string VARCHAR(10);
SET @date_string = CONVERT(VARCHAR(10), @eighteen_months_ago, 120); -- 120 represents the 'yyyy-MM-dd' format

-- Delete data from the table where 'date_column' is older than 18 months
DELETE FROM your_table_name WHERE CONVERT(VARCHAR(10), date_column, 120) < @date_string;

rrxvtqn