MySQL Querying Data

Retrieving Data using SELECT Statements:

  • The SELECT statement is used to retrieve data from one or more tables.
                      
                      SELECT column1, column2 FROM table_name;
                      
                    

Filtering Data:

  • You can use the WHERE clause to filter rows based on specified conditions.
                      
                      SELECT * FROM table_name WHERE condition;
                      
                    

Sorting Data:

  • You can use the ORDER BY clause to sort the result set based on one or more columns, either in ascending (ASC) or descending (DESC) order.
                      
                      SELECT * FROM table_name ORDER BY column1 ASC;
                      
                    

Aggregate Functions for Data Analysis:

  • COUNT: Counts the number of rows in a result set.
  • SUM: Calculates the sum of values in a column.
  • AVG: Calculates the average value of a column.
  • MIN: Retrieves the minimum value in a column.
  • MAX: Retrieves the maximum value in a column.
                      
SELECT COUNT(*) FROM table_name;
SELECT SUM(column1) FROM table_name;
SELECT AVG(column2) FROM table_name WHERE condition;
                      
                    

Putting it Together:

By combining these components, you can make queries that are quite reliouos.

Likewise, one can select the necessary columns from a list by means of specific filters, sort them by order and calculate the aggregate of it for data analysis.

                      
SELECT column1, column2, SUM(column3) AS total_sales 
FROM sales 
WHERE year = 2023 
GROUP BY column1 
ORDER BY total_sales DESC;