SQL WITH Clause | How to write SQL Queries using WITH Clause | SQL CTE (Common Table Expression)

SQL WITH Clause | How to write SQL Queries using WITH Clause | SQL CTE (Common Table Expression)

techTFQ

2 года назад

546,100 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

techTFQ
techTFQ - 04.06.2022 17:40

Please note the COST column used in this table represents the total cost of purchase hence we do not need to multiple quantity with cost to get the total cost. I designed the dataset with an assumption that COST is the total purchase cost.

Ответить
Data Analyst
Data Analyst - 21.10.2023 15:45

How can I get the data set used in this video as in your blogs it is not available?

Ответить
Data Analyst
Data Analyst - 21.10.2023 15:04

21/10/23

Ответить
Ankit Kumar
Ankit Kumar - 21.10.2023 14:20

-- 1
select * from(select id,store_name,sum(cost) as sales from apple
group by id,store_name)t
where t.sales >= (select avg(a.c_sum)from(select id,sum(cost) as c_sum from apple
group by id) a);
-- 2
with s as (select id,store_name,sum(cost) as sales from apple
group by id,store_name)
select * from s
where sales >= (select avg(sales) from s);

Ответить
Cloudy Gamer
Cloudy Gamer - 17.10.2023 08:59

----- Simplified Query -------- without so many confusing alias -_-
Question :: find stores whose sales are better than the average sales across all the stores
-- total sales per store
-- avg sales across all the stores
-- comparison

Answer::

with total_sales (store_name, total_sales_per_store) as
(select store_name, sum(price) from sales group by store_name),

avg_sales (avg_sales_across_all_stores) as
(select cast(avg(total_sales_per_store) as int) from total_sales)

select * from total_sales ts
Join avg_sales av
on ts.total_sales_per_store > av.avg_sales_across_all_stores

Ответить
Cloudy Gamer
Cloudy Gamer - 17.10.2023 07:16

way too fast... my brain can't process these many information in 2-3 sec

Ответить
Mohd. Israr
Mohd. Israr - 12.10.2023 21:09

Great explanation sir, can you please make 1 video on group concate in deep 😊

Ответить
dockerbuild
dockerbuild - 04.10.2023 09:33

oh how rare seeing a programmer with ok english accent

Ответить
Rk_fitness
Rk_fitness - 03.10.2023 15:55

i have never seen someone like you to be that perfect in sql ever. hats off taufiq ,god bless you SIR♥

Ответить
sai swaroop
sai swaroop - 03.10.2023 09:30

Your Explanations were concise and on point. very clear explanations. youre a perfect guide for SQL @techTFQ Thanks!!

Ответить
Syed Mahiyan
Syed Mahiyan - 02.10.2023 08:05

Your Link is not working to Download all the scripts

Ответить
Rajarshi Basu
Rajarshi Basu - 23.09.2023 13:00

So when to use with Clause and When to use Sub Query?

Ответить
swarnalathak88
swarnalathak88 - 20.09.2023 21:58

GRATITUDE for all the videos in sql complex queries.

Ответить
Raihan
Raihan - 18.09.2023 03:21

Hi Taufiq,
can you please tell me why my cast function is giving me error, when I am trying to cast it to Integer?

select cast(avg(salary)as int) as abc from employees;

Ответить
Coding is Life
Coding is Life - 10.09.2023 12:03

thanks sir. May u live long.

Ответить
Vikash kumar Mahato
Vikash kumar Mahato - 10.09.2023 11:56

the script given in description cannot be found

Ответить
sripada palai
sripada palai - 09.09.2023 18:32

sir u are great,ur learning procedure awasome

Ответить
Arka Sarkar
Arka Sarkar - 07.09.2023 11:49

Query 2 code : create table sales(
store int,
store_name varchar(256),
product varchar(256),
quantity int,
cost int
);
insert into sales(store,store_name,product,quantity,cost) values
(1, 'apple originals 1', 'iPhone 12 Pro', 1, 1000),
(1, 'apple originals 1', 'Macbook Pro 13', 3, 2000),
(1, 'apple originals 1', 'Airpods Pro', 2, 280),
(2, 'apple originals 2', 'iPhone 12 Pro', 2, 1000),
(3, 'apple originals 3', 'iPhone 12 Pro', 1, 1000),
(3, 'apple originals 3', 'Macbook Pro 13', 1, 2000),
(3, 'apple originals 3', 'Macbook Air', 4, 1100),
(3, 'apple originals 3', 'iPhone 12', 2, 1000),
(3, 'apple originals 3', 'Airpods Pro', 2, 280),
(4, 'apple originals 4', 'iPhone 12 Pro', 2, 1000),
(4, 'apple originals 4', 'Macbook Pro 13', 1, 2500);

Ответить
Saurav Labade
Saurav Labade - 06.09.2023 14:16

You should give the tables link in the description box .It will be better

Ответить
Mayur Joshi
Mayur Joshi - 01.09.2023 20:08

No need to use join instead use where condition and compare one subquery with other

Ответить
Jaganath Adishesha
Jaganath Adishesha - 30.08.2023 12:10

Need table details

Ответить
VENKATESWARA RAO Devisetti
VENKATESWARA RAO Devisetti - 26.08.2023 08:27

if u can post some videos on query optimization
that will be great

Ответить
Yssa Bronzal
Yssa Bronzal - 20.08.2023 07:41

Good day! Where can I get the data? The URL has an error when I opened it.

Ответить
Muhammad Uzair Awan
Muhammad Uzair Awan - 15.08.2023 13:44

Excellent Tutorial

Ответить
Mustafa
Mustafa - 14.08.2023 09:49

This is soooo helpful!! Thank you Thoufiq, you are a God sent :D

Ответить
Parikshit Kaushal
Parikshit Kaushal - 13.08.2023 21:20

Incredible teaching!!

Ответить
sindhuja alagarsamy
sindhuja alagarsamy - 09.08.2023 07:46

Hi Thoufiq
Thank you so much for great and very clear explanation on SQL and career for DATA ANALYST. I learnt SQL concepts from your channel, i can easily understand concepts but when i practice in leetcode i am unable to do what function should be used and how to follow the steps to find the solution. Can you please make a video which describes how see the problem and do it in step by step order?

Ответить
Harish Harry
Harish Harry - 07.08.2023 21:21

😊😊thank you so much

Ответить
Mullai Pugazhendi
Mullai Pugazhendi - 07.08.2023 10:39

CLEAR AND SIMPLE. LOVELY EXPLANATION

Ответить
Vineeta Singh
Vineeta Singh - 28.07.2023 22:09

Hi Tawfeeq @techTFQ really grateful to have found your videos' on SQL, no one could have explained better and so much in depth. Do you also conduct online bootcamps on SQL / Python? Would like to learn from you.

Ответить
Marked By Diaspora.
Marked By Diaspora. - 28.07.2023 21:07

very confusing first five minutes into the video. Thanks though.

Ответить
palance
palance - 22.07.2023 08:49

this is interesting topic , i enjoyed the video while practicing it on my own as well.
(and yours ears are like PK😅😁)

Ответить
Kalluri Yaswanth Kumar
Kalluri Yaswanth Kumar - 04.07.2023 14:23

cant we alias total sales in without with clause query @techtfq

Ответить
Asad Ullah
Asad Ullah - 02.07.2023 02:50

Can someone write a sql query of how many time 'basically' is used in the video?

Ответить
Rajesh Banka
Rajesh Banka - 26.06.2023 04:38

Nice explanation

Ответить
MD Hossain
MD Hossain - 23.06.2023 05:00

Bhai thorasa slow Bolna.

Ответить
Mohamed Saymudeen
Mohamed Saymudeen - 23.06.2023 04:36

I have a doubt, In line no. 15, why we are giving alias X, I tried without alias, it was throwing an error like "every derived table must have its own alias", what would be the reason behind it?

Please help me to get a clear picture

Ответить
dinesh panihar
dinesh panihar - 22.06.2023 01:45

Everything is good but only problem Sir you're speaks very fast please teach in slow mode😝

Ответить
Bharat Rathod
Bharat Rathod - 13.06.2023 17:05

Excellent video, I am learning lot of things from you. If you can also provide some practice questions as well like one you solve in your videos. Thank you so much for such very well explained conceptual SQL videos.

Ответить
bananaboydan
bananaboydan - 06.06.2023 20:35

Best video i have sen for this concept!

Ответить
Estero
Estero - 06.06.2023 09:26

May Allah Bless You Sir ❤

Ответить
Swapnasis Mohanty
Swapnasis Mohanty - 30.05.2023 02:05

I am trying to run this below query --
with empt as (Select First_name from employees)
but showing the below error
(ERROR: syntax error at end of input
LINE 1: with empt as (Select First_name from employees)
^
SQL state: 42601
Character: 49)
----


Can you please let me know what am I doing wromng?

Ответить
RiPp1er
RiPp1er - 26.05.2023 02:53

Nice explanation of CTEs!

Ответить
akhilesh r
akhilesh r - 24.05.2023 21:09

awesome😮

Ответить
MANAF
MANAF - 18.05.2023 16:13

Really Helpful and Very Good to understand SQL especially for Beginners

Ответить
Vivek K Bangaru
Vivek K Bangaru - 12.05.2023 20:43

Really awesome videos man thanks for this session.

Ответить
Santosh Anjani
Santosh Anjani - 11.05.2023 09:08

Hello sir, thanks for your help to make us understand the CTE in most easy and best way.

I would like to know if is there any sql server complete course you are providing on udemy or somewhere?

Ответить
Electro Arzoon
Electro Arzoon - 11.05.2023 07:22

Nice

Ответить
unv unb
unv unb - 10.05.2023 16:36

Very good video

Ответить