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
βοΈ 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.
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.
*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
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.
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;
βοΈ 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;
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
SQL - A Beginner-friendly version! π
(Clauses: IN, BETWEEN and LIKE)
Thread π§΅
πWhich table are we working on today?
The name of the table is "products". So the columns that we have are productCode, productName, productDescription, among others.
Take a look π
βοΈ IN
This clause would reduce the efforts to write multiple OR conditions. It helps you find a specific match for a value.
π select column1, column2 from table_name where EXPRESSION IN ('value1', 'value2')
Hey, folks! Some time back, I was scrolling through LinkedIn and found some essential SQL topics to revisit before an Interview.
Have a look π§΅π
1. WHERE, AND, OR, NOT, IN 2. ORDER BY, ASC, DESC 3. IS NULL 4. LIMIT 5. MIN, MAX, COUNT, AVG, SUM 6. LIKE, WILDCARDS 7. IN BETWEEN 8. INNER JOIN 9. LEFT JOIN
10. UNION ALL 11. GROUP BY 12. HAVING 13. LEFT, RIGHT, MID, CONCAT 14. PARTITION BY, OVER 15. LEAD, LAG 16. Subqueries 17. RANK, DENSE_RANK, PERCENT_RANK 18. ROW_NUMBER, CUME_DIST 19. FIRST_VALUE, LAST_VALUE 20. AS
Let's dive further into SQL to help you through the most demanding skill required in the Data Science Industry.
A Beginner's Guide to Filter Results in MySQL - Part 5!!
Thread π§΅
βοΈ LIMIT Clause
To restrict the number of rows in the final result, we use the LIMIT clause.
That means if you have 10,000 rows in the data, you can fetch only 10 rows using this clause in your query.
π Query: select * table_name LIMIT limit_no;
For e.g., Consider a table PAYMENTS. It has more than 1000 rows. [See the image]