Sukriti Macker Profile picture
Jun 24, 2021 β€’ 13 tweets β€’ 4 min read β€’ Read on X
Here is another thread of the most important clauses to boost your SQL queries to the next level!

Advance SQL, A beginner-friendly version! (#DataScience)

Thread 🧡
Let me explain what the table looks like.
The table name is customer_list, where the columns are ID, name, address, zip code, phone, city, country, notes & SID.

See the image for reference The table and column names in that table.
β˜‘οΈ GROUP BY
This clause is used to group rows that have the same values together. It summarises data from the database.

Note: The group by clause returns one row for each group.

πŸ‘‰ Query: select col_name from table_name GROUP BY column_name;
Example: Group the customers by country.
-> select country, count(*) from customer_list group by country;

Here we get the number of customers in each country. For ex: Argentina has 13 customers, Austria 3 and so on. group by query 1
Let's implement another query with the same clause.

Query: Find the number of customers in each country where the state is not null.

Simplifying the query: Our aim is to find the number of customers where the state is NOT NULL grouped by country.
-> select country, count(*) from customers_list where city is not null GROUP BY country;

See the image for the output. group by query 2
*Important*
How to group the results based on two columns? πŸ€”

Query: Count the no. of customers first based on country, then based on city.

SQL query: select country, city, count(*) from customer_list group by country, city;

See the image for output grouping 2 columns using "group by"
Let me simplify this:-
The above query will be executed in two parts.

First, we will get the count of customers based on countries based on country

Then, corresponding to each country we will get each city in the country & the no. of customers in that city. 2 columns group by
Observe the output of the above query.

Country Austria has two cities Linz and Salzburg. Linz has 1 customer, and Salzburg has 1 Customer.

See the flowchart for a better understanding.
β˜‘οΈ HAVING
The clause WHERE cannot be used to filter the "grouped" results obtained using the GROUP BY clause.

We use the HAVING clause for that.

Note: If the group by clause is not present the HAVING clause behaves like a WHERE clause.
Query: Count the number of customers with respect to countries where the no. of customers is greater than 8.

SQL query: select country, count(*) from customer_list group by country HAVING count(country)>8; HAVING clause
β˜‘οΈ ORDER BY
The clause is used to sort the results in ascending or descending order.

It sorts the result in ascending order by default. To sort the result in descending order, use the keyword DESC.
Query: Count the number of customers with respect to countries where the no. of customers is greater than 8 and sort the countries in descending order.

SQL query: select country, count(*) from customer_list group by country HAVING count(country)>8 order by country desc; Order by clause

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Sukriti Macker

Sukriti Macker Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Sukriti_Macker

Apr 4, 2022
Machine Learning Roadmap for BEGINNERS with resources!! πŸ€–πŸ§΅

1. Study mathematical concepts:-
a) Linear Algebra
b) Calculus
c) Probability
d) Statistics

2. Pick your programming language (preferably Python)
3. Start of with the basic Machine Learning Algos

4. Projects

5. Practice, practice, practice

Resources πŸ‘‡πŸ§΅
A) Linear Algebra
πŸ”— khanacademy.org/math/linear-al…

B) Calculus
πŸ”— youtube.com/playlist?list=…

C) Probability
πŸ”— edx.org/course/probabi…

D) Statistics
πŸ”— Notes: mathsbox.org.uk/twi/astats.pdf

πŸ”— Lec:
Read 5 tweets
Jan 31, 2022
Let's talk about R, shall we? πŸ‘©β€πŸ’»

8 Top Courses (FREE & Certification) for R Programming!! πŸ‘‡ 🧡
➑️ R Programming by @freeCodeCamp
Free 2 Hours course to get you started!

πŸ”—
➑️ R Programming by @simplilearn
Free 7 Hours course. Great for Beginners!

πŸ”—
Read 10 tweets
Jan 24, 2022
Statistics plays a crucial role in Data Science!! πŸ“Š

Do you should know how to understand & interpret your data?? πŸ€”

Top FREE Courses for Statistics you must check out πŸ‘‡πŸ§΅
➑️ FreeCodeCamp on YouTube

πŸ”—
➑️ KhanAcademy
This learning platform is the best! I just love it.

πŸ‘‰ High School Level Stats (for foundation)
πŸ”— khanacademy.org/math/probabili…
Read 7 tweets
Jan 11, 2022
Being in the industry of data science and machine learning, I cannot stress the importance of knowing Microsoft Excel. 🀯

Top 4 Free MS Excel Resources πŸ‘‡ 🧡
➑️ Microsoft Excel by freeCodeCamp

➑️ Microsoft Excel by Edureka

Read 6 tweets
Nov 27, 2021
Looking for Linux Command Line Courses?
Look no further! 🀩

Top 4 Free Linux Command Line Courses πŸ‘‡πŸ‘‡
Some might not know the answer to:-

What is Linux?? ➑️ linux.com/what-is-linux/
➑️ Linux Commands by Edureka
πŸ”—
Read 7 tweets
Nov 20, 2021
Learn Git and GitHub together!

Some of you may be new to the programming world. Don't worry! I got you. πŸš€

5 Top FREE resources for Git & GitHub πŸ‘‡ 🧡
➑️ Git and GitHub for Beginners - Crash Course
πŸ”—
➑️ Git & GitHub Crash Course For Beginners
πŸ”—
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(