Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Thursday, 1 June 2023

MySQL Aggregate Functions

MySQL aggregate functions are used to perform calculations on multiple values and return a single value. Some of the most common aggregate functions are:


COUNT(): Returns the number of rows in a group.

SUM(): Returns the sum of all values in a group.

AVG(): Returns the average of all values in a group.

MIN(): Returns the minimum value in a group.

MAX(): Returns the maximum value in a group.


Aggregate functions are often used with the GROUP BY clause to group rows together based on a common value. For example, the following query will return the number of orders for each product:


SQL

SELECT product_id, COUNT(*) AS num_orders

FROM orders

GROUP BY product_id;


The COUNT() function will count the number of rows in each group, and the GROUP BY clause will group the rows together based on the product_id column.


Aggregate functions can also be used without the GROUP BY clause. In this case, the aggregate function will be applied to all rows in the table. For example, the following query will return the average price of all products:


SQL

SELECT AVG(price) AS avg_price

FROM products;


The AVG() function will calculate the average price of all products in the products table.


I hope this helps! Let me know if you have any other questions.

MySQL Join Query

A MySQL join query is used to combine rows from two or more tables, based on a related column between them. There are four types of joins in MySQL:


Inner join: Returns all rows from both tables where the join condition is satisfied.

Left join: Returns all rows from the left table, and the matched rows from the right table.

Right join: Returns all rows from the right table, and the matched rows from the left table.

Cross join: Returns all possible combinations of rows from both tables.


The syntax of a MySQL join query is as follows:


SELECT column_list

FROM table_name1

[INNER | LEFT | RIGHT | OUTER] JOIN table_name2

ON table_name1.column_name = table_name2.column_name

[WHERE condition]

[GROUP BY column_name]

[HAVING condition]

[ORDER BY column_name [ASC | DESC]]

[LIMIT number_of_rows]


The column_list is a list of columns that you want to retrieve. You can specify all columns by using the asterisk (*), or you can specify specific columns.


The table_name1 and table_name2 are the names of the tables that you want to join.


The INNER, LEFT, RIGHT, and OUTER keywords specify the type of join that you want to perform.


The ON clause specifies the join condition.


The WHERE clause is used to specify a condition that must be met for a row to be included in the result set.


The GROUP BY clause is used to group rows together based on a common value.


The HAVING clause is used to specify a condition that must be met for a group to be included in the result set.


The ORDER BY clause is used to sort the result set by a column.


The LIMIT clause is used to limit the number of rows that are returned.


Here are some examples of MySQL join queries:



SELECT * FROM users INNER JOIN orders ON users.id = orders.user_id;


This query will retrieve all rows from the users table and the orders table where the id column in the users table is equal to the user_id column in the orders table.



SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id;


This query will retrieve all rows from the users table, and the matched rows from the orders table. If there are no matched rows in the orders table, the orders table will have NULL values in the columns that are joined.


SELECT * FROM users RIGHT JOIN orders ON users.id = orders.user_id;


This query will retrieve all rows from the orders table, and the matched rows from the users table. If there are no matched rows in the users table, the users table will have NULL values in the columns that are joined.


SELECT * FROM users CROSS JOIN orders;


This query will return all possible combinations of rows from the users table and the orders table.


I hope this helps! Let me know if you have any other questions.