Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Most used

MySQL query

By Ariful Asfake Sunny


Web Programmer and Product Designer
Queries that used most often in MySQL
➔ SELECT All
➔ SELECT Column
➔ WHERE clause
➔ CREATE Table
➔ INSERT INTO
➔ UPDATE
➔ DELETE FROM
➔ GROUP BY
➔ HAVING Clause
➔ Aggregate Functions (SUM, AVG & COUNT)
➔ JOINS

Overview Index of Slide


SELECT All (*)
A simple SELECT query is used to fetch and send back all the data that’s
from a database by using Asterisk(*).

Syntax :
SELECT * FROM Table_name;

1
SELECT Column
To get data from specific column(s) and not everything, or you simply
want to just state the field name.

Syntax :
SELECT Column_name
FROM Table_name;

2
WHERE Clause
WHERE clause is used as a condition for specific records that match the
condition(s) as the result.

Syntax :
SELECT *
FROM Table_name
WHERE EmployeeID = 1008 ;

3
CREATE Table
This query can be used to generate a new table with your rpreferred
choice of columns. When adding column names be sure to specify their
data type.

Syntax :
CREATE TABLE Table_name(
Id INT,
Department VARCHAR(55),
);
4
INSERT INTO
Query to insert values into one or more rows of new records into a table.
When writing an INSERT INTO query, the VALUES command must be a
part of the complete statement.

Syntax :
INSERT INTO EmployeeInfo (ID, Department)
VALUES
(1002, HR , 46)
(1004, IT , 39);
5
UPDATE
This keyword is used to change one or more existing columns in a table.
Using this query will update table records with new data based on some
condition. Following the UPDATE command, use the keyword SET to always
specify which column(s) you choose to modify, then state exactly WHERE
you need the updated data applied to.

Syntax :
UPDATE Existing_table_name
SET Age = 22
WHERE Id = 1008;
6
DELETE FROM
To remove records from a table based on one or multiple conditions you
would need to use the DELETE FROM statement. Use WHERE condition
to determine exactly where you need the updated data applied to.

Syntax :
DELETE FROM Existing_table_name
WHERE Name = ‘Sunny’;

7
GROUP BY & HAVING Clause
Using the GROUP BY clause will group together the result-set by one or
more columns. The HAVING keyword is used to filter that result set.

Syntax :
SELECT COUNT(Age), Id
FROM Existing_table_name
GROUP BY Id
HAVING COUNT(Age) > 25;
8
Aggregate Functions (COUNT)
There are three extremely common aggregate functions that allow data
from a table to be interpreted or run calculations.

COUNT: returns the number of rows that matches a specified column.

Syntax :
SELECT
COUNT(Department)
FROM Existing_table_name;
9
Aggregate Functions (AVG)
There are three extremely common aggregate functions that allow data
from a table to be interpreted or run calculations.

AVG: returns the average value of a column.

Syntax :
SELECT AVG(Age)
FROM Existing_table_name;

9
Aggregate Functions (SUM)
There are three extremely common aggregate functions that allow data
from a table to be interpreted or run calculations.

SUM: returns the sum of all the values in a selected column.

Syntax :
SELECT SUM(Age)
FROM Existing_table_name;

9
JOINS
Joins are used to bring together rows from at least two tables, based on a
related column between the tables. The most common practice joins are
INNER, FULL and LEFT.

INNER JOIN – combines the rows from different tables if the join condition
is true.
FULL JOIN – returns all rows when there is a match in left or right table
records.
LEFT JOIN – retrieves all rows from the left table and the matching records
from the right table.

8
INNER JOIN

Syntax :

SELECT Student.Title, StudentInfo.Department


FROM Student
INNER JOIN StudentInfo
ON Student.Id = StudentInfo.Info_Id;

8
FULL JOIN

Syntax :

SELECT *
FROM Student
FULL JOIN StudentInfo
ON Student.Id = StudentInfo.Info_Id;

8
LEFT JOIN

Syntax :

SELECT Student.Name, StudentInfo.Age


FROM Student
LEFT JOIN StudentInfo
ON Student.Id = StudentInfo.Info_Id;

8
Thanks For Stopping By

You might also like