We Learn SQL #15 | SQL Subqueries in SELECT
HTML-код
- Опубликовано: 25 авг 2020
- 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.
If you want to learn or practice SQL, check out learnsql.com/.
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!
👌 Join us on Facebook: / welearnsql
🔊 Music: www.bensound.com/ - Наука
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.
Thanks. Never learn this method before.
Please lower down the volume of background music
What if we need column name , instead of aggregates ?
Thanks for the tutorial. Very helpful.
legendary lad
thank u
Teşekkürler dostum
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!
I didn't find it distracting - but it didn't help me feel like I was understanding everything. I had to pause the video in several places to digest what it said - so the music was not useful because it also got paused
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;