Some SQL Aggregate Functions that would come in handy! ➕ ➖ ➗ ✖️
Let's dive right into them. 🤩
Thread 🧵
☑️ Aggregate Functions
The motive of these functions is to perform a calculation on a set of values and return a single value as a result.
Different functions:- 1. COUNT 2. AVERAGE (AVG) 3. MAXIMUM (MAX) 4. MINIMUM (MIN) 5. SUM
To understand most of the Advance SQL clauses, we will use the table named "payment". It consists of columns like payment_id, amount, staff_id, customer_id, etc. (See the image).
The first 5 rows of the table are also described for reference.
☑️ Count 1. To count all the rows in the table
👉 Query: select count(*) from table_name;
Note: count(*) includes the count of null values as well.
2. To count the rows in a specific column
👉 Query: select count(column_name) from table_name;
Note: count(column_name) does NOT include the count of null values.
3. To count the no. of distinct values in a specific column
👉 Query: select count(DISTINCT column_name) from table_name;
Let us see the examples of all three queries above:-
1. select count(*) from payment;
Op: 16049 [This ans includes NULL values]
2. select count(rental_id) from payment;
Op: 16044
3.
a) select count(customer_id) from customer_list;
Op: 16049
b) select count(distinct customer_id) from customer_list;
To find the number of unique customer_id
Op: 599
The total number of customer_id is 16049, out of which only 599 are DISTINCT.
☑️ Average
Gives the average value for numeric data.
Note: For "avg" of any non-numeric column will not give an error; the result will be a ZERO.
Query: select AVG(column_name) from table_name;
For example:-
select avg(amount) from payment;
-> Calculating average amount from table payment
☑️ Maximum, Minimum
To find the max value and min value from a column.
Query (max): select max(col_name) from table_name;
Query (min): select max(col_name) from table_name;
Example:
(Max): select max(amount) from payment;
(Min): select min(amount) from payment;
☑️ Sum
To calculate the sum of all the values.
Query: select sum(column_name) from table_name;
• • •
Missing some Tweet in this thread? You can try to
force a refresh
I hope you are practising SQL whilst you are learning it. This technique makes it easier to understand concepts.
Alrighty, here is another thread for SQL. 🤩
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')
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;
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]