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 🧵
Advance SQL, A beginner-friendly version! (#DataScience)
Thread 🧵
☑️ 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;
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;
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.
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.
☑️ 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.
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.
☑️ 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.
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.
Loading suggestions...