0% found this document useful (0 votes)
26 views

Database

Uploaded by

CALLMEPRO
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Database

Uploaded by

CALLMEPRO
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

What is Database?

A database is an organized collection of data, so that it can be easily accessed and managed. You can
organize data into tables, rows, columns, and index it to make it easier to find relevant information.

There are many databases available like MySQL, Sybase, Oracle, MongoDB, Informix, PostgreSQL, SQL
Server, etc.

MySQL

MySQL is a relational database management system based on the Structured Query Language, which is the
popular language for accessing and managing the records in the database. MySQL is open-source and free
software under the GNU license. It is supported by Oracle Company.

MySQL CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a new SQL database.

Syntax : CREATE DATABASE databasename;

Example : CREATE DATABASE testDB;

MySQL DROP DATABASE Statement

The DROP DATABASE statement is used to drop an existing SQL database.

Syntax : DROP DATABASE databasename;

Example : DROP DATABASE testDB;

MySQL CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in a database.

Syntax :

CREATE TABLE table_name (

column1 datatype,

column2 datatype,

column3 datatype,

....

);
Example :

CREATE TABLE Persons (

PersonID int,

LastName varchar(255),

FirstName varchar(255),

Address varchar(255),

City varchar(255)

);

Create Table Using Another Table

 A copy of an existing table can also be created using CREATE TABLE.


 The new table gets the same column definitions. All columns or specific columns can be selected.
 If you create a new table using an existing table, the new table will be filled with the existing values
from the old table.

Syntax :

CREATE TABLE new_table_name AS

SELECT column1, column2,...

FROM existing_table_name

WHERE ....;

The following SQL creates a new table called "TestTables" (which is a copy of the "Customers" table):

Example :

CREATE TABLE TestTable AS

SELECT customername, contactname

FROM customers;

MySQL DROP TABLE Statement

The DROP TABLE statement is used to drop an existing table in a database.

Syntax : DROP TABLE table_name;

Example : DROP TABLE Shippers;


MySQL TRUNCATE TABLE

The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself.

Syntax : TRUNCATE TABLE table_name;

MySQL ALTER TABLE Statement

 The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
 also used to add and drop various constraints on an existing table.

ALTER TABLE - ADD Column

To add a column in a table, use the following syntax:

Syntax : ALTER TABLE table_name

ADD column_name datatype;

The following SQL adds an "Email" column to the "Customers" table:

Example : ALTER TABLE Customers

ADD Email varchar(255);

ALTER TABLE - DROP COLUMN

To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):

Syntax : ALTER TABLE table_name

DROP COLUMN column_name;

The following SQL deletes the "Email" column from the "Customers" table:

Example : ALTER TABLE Customers

DROP COLUMN Email;

ALTER TABLE - MODIFY COLUMN

To change the data type of a column in a table, use the following syntax:

Syntax : ALTER TABLE table_name

MODIFY COLUMN column_name datatype;


MySQL ALTER TABLE Example

Look at the "Persons" table:

ID LastName FirstName Address City


1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Now we want to add a column named "DateOfBirth" in the "Persons" table.


We use the following SQL statement:

Example : ALTER TABLE Persons


ADD DateOfBirth date;
The "Persons" table will now look like this:
ID LastName FirstName Address City DateOfBirth
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Change Data Type Example


 Now we want to change the data type of the column named "DateOfBirth" in the "Persons" table.
 We use the following SQL statement:

Example : ALTER TABLE Persons


MODIFY COLUMN DateOfBirth year;
Notice that the "DateOfBirth" column is now of type year and is going to hold a year in a two- or four-digit
format.
DROP COLUMN Example
 Next, we want to delete the column named "DateOfBirth" in the "Persons" table.
 We use the following SQL statement:

Example : ALTER TABLE Persons


DROP COLUMN DateOfBirth;
The "Persons" table will now look like this:
ID LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
MySQL SELECT Statement
 The SELECT statement is used to select data from a database.
 The data returned is stored in a result table, called the result-set.

SELECT Syntax : SELECT column1, column2, ...


FROM table_name;
 Here, column1, column2, ... are the field names of the table you want to select data from.
 If you want to select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;

Demo Database - "Customers" table

Customer
CustomerName ContactName Address City PostelCode Country
ID
Berglunds
1 Alfreds Futterkiste Maria Anders snabbköpObere Berlin 12209 Germany
Str. 57
Ana Trujillo
Avda. de la
2 Emparedados y Ana Trujillo México D.F. 05021 Mexico
Constitución 2222
helados
Antonio Moreno Antonio
3 Mataderos 2312 México D.F. 05023 Mexico
Taquería Moreno
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds Christina
5 Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp Berglund

SELECT Columns Example


SELECT CustomerName, City, Country FROM Customers;

CustomerName City Country

Alfreds Futterkiste Berlin Germany


Ana Trujillo Emparedados y
México D.F. Mexico
helados
Antonio Moreno Taquería México D.F. Mexico

Around the Horn London UK

Berglunds snabbköp Luleå Sweden

SELECT * Example
The following SQL statement selects ALL the columns from the "Customers" table:

SELECT * FROM Customers;


Customer
CustomerName ContactName Address City PostelCode Country
ID
Berglunds
1 Alfreds Futterkiste Maria Anders snabbköpObere Berlin 12209 Germany
Str. 57
Ana Trujillo
Avda. de la
2 Emparedados y Ana Trujillo México D.F. 05021 Mexico
Constitución 2222
helados
Antonio Moreno Antonio
3 Mataderos 2312 México D.F. 05023 Mexico
Taquería Moreno
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds Christina
5 Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp Berglund

MySQL SELECT DISTINCT Statement

 The SELECT DISTINCT statement is used to return only distinct (different) values.
 Inside a table, a column often contains many duplicate values; and sometimes you only want to list
the different (distinct) values.

Syntax : SELECT DISTINCT column1, column2, ...


FROM table_name;

Example : SELECT DISTINCT Country FROM Customers;

The following SQL statement counts and returns the number of different (distinct) countries in the
"Customers" table:

Example : SELECT COUNT(DISTINCT Country) FROM Customers;

Output : 4

MySQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax

 It is possible to write the INSERT INTO statement in two ways:


1. Specify both the column names and the values to be inserted:
2. If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table. Here, the INSERT INTO syntax would be as follows:

1). INSERT INTO table_name (column1, column2, column3, ...)

VALUES (value1, value2, value3, ...);


2). INSERT INTO table_name

VALUES (value1, value2, value3, ...);

Example :

INSERT INTO Customers (CustomerID, CustomerName, ContactName, Address, City, PostalCode,


Country) VALUES (6,'Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

Customer
CustomerName ContactName Address City PostelCode Country
ID
Berglunds
1 Alfreds Futterkiste Maria Anders snabbköpObere Berlin 12209 Germany
Str. 57
Ana Trujillo
Avda. de la
2 Emparedados y Ana Trujillo México D.F. 05021 Mexico
Constitución 2222
helados
Antonio Moreno Antonio
3 Mataderos 2312 México D.F. 05023 Mexico
Taquería Moreno
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds Christina
5 Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp Berglund
Tom
6 Cardinal Skagen 21 Stavanger 4006 Norway
B.Erichsen

Insert Data Only in Specified Columns

 It is also possible to only insert data in specific columns.


 The following SQL statement will insert a new record, but only insert data in the "CustomerName",
"City", and "Country" columns (CustomerID will be updated automatically):

Example :

INSERT INTO Customers (CustomerName, City, Country)

VALUES ('Cardinal', 'Stavanger', 'Norway');

MySQL WHERE Clause

 The WHERE clause is used to filter records.


 It is used to extract only those records that fulfill a specified condition.

Syntax : SELECT column1, column2, ...

FROM table_name

WHERE condition;
Example : SELECT * FROM Customers

WHERE Country = 'Mexico';

Customer
CustomerName ContactName Address City PostelCode Country
ID
Ana Trujillo
Avda. de la
2 Emparedados y Ana Trujillo México D.F. 05021 Mexico
Constitución 2222
helados
Antonio Moreno Antonio
3 Mataderos 2312 México D.F. 05023 Mexico
Taquería Moreno

Text Fields vs. Numeric Fields

 SQL requires single quotes around text values (most database systems will also allow double quotes).
 However, numeric fields should not be enclosed in quotes:

Example : SELECT * FROM Customers

WHERE CustomerID = 1;

Customer
CustomerName ContactName Address City PostelCode Country
ID
Berglunds
1 Alfreds Futterkiste Maria Anders snabbköpObere Berlin 12209 Germany
Str. 57

The following operators can be used in the WHERE clause:

Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column
MySQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

Syntax : UPDATE table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

UPDATE Table

 The following SQL statement updates the first customer (CustomerID = 1) with a new contact
person and a new city.

Example : UPDATE Customers

SET ContactName = 'Alfred Schmidt', City = ' México D.F.'

WHERE CustomerID = 2;

Customer
CustomerName ContactName Address City PostelCode Country
ID
Berglunds
1 Alfreds Futterkiste Maria Anders snabbköpObere Berlin 12209 Germany
Str. 57
Avda. de la
2 Alfred Schmidt Ana Trujillo México D.F. 05021 Mexico
Constitución 2222
Antonio Moreno Antonio
3 Mataderos 2312 México D.F. 05023 Mexico
Taquería Moreno
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds Christina
5 Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp Berglund
Tom
6 Cardinal Skagen 21 Stavanger 4006 Norway
B.Erichsen

UPDATE Multiple Records

 It is the WHERE clause that determines how many records will be updated.
 The following SQL statement will update the PostalCode to 00000 for all records where
country is "Mexico":

Example : UPDATE Customers

SET PostalCode = 00000

WHERE Country = 'Mexico';


Customer
CustomerName ContactName Address City PostelCode Country
ID
Berglunds
1 Alfreds Futterkiste Maria Anders snabbköpObere Berlin 12209 Germany
Str. 57
Avda. de la
2 Alfred Schmidt Ana Trujillo México D.F. 00000 Mexico
Constitución 2222
Antonio Moreno Antonio
3 Mataderos 2312 México D.F. 00000 Mexico
Taquería Moreno
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds Christina
5 Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp Berglund
Tom
6 Cardinal Skagen 21 Stavanger 4006 Norway
B.Erichsen

Update Warning!

Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!

Example : UPDATE Customers

SET PostalCode = 00000;

MySQL DELETE Statement

The DELETE statement is used to delete existing records in a table.

Syntax : DELETE FROM table_name WHERE condition;

Example : DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

Customer
CustomerName ContactName Address City PostelCode Country
ID
Avda. de la
2 Alfred Schmidt Ana Trujillo México D.F. 00000 Mexico
Constitución 2222
Antonio Moreno Antonio
3 Mataderos 2312 México D.F. 00000 Mexico
Taquería Moreno
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds Christina
5 Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp Berglund
Tom
6 Cardinal Skagen 21 Stavanger 4006 Norway
B.Erichsen

Delete All Records

It is possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will be intact:

Syntax : DELETE FROM table_name;


The following SQL statement deletes all rows in the "Customers" table, without deleting the table:

Example : DELETE FROM Customers;

MySQL ORDER BY Keyword

 The ORDER BY keyword is used to sort the result-set in ascending or descending order.
 The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.

Syntax : SELECT column1, column2, ...

FROM table_name

ORDER BY column1, column2, ... ASC|DESC;

Example : SELECT * FROM Customers

ORDER BY Country;

The following SQL statement selects all customers from the "Customers" table, sorted DESCENDING by the
"Country" column:

Example : SELECT * FROM Customers

ORDER BY Country DESC;

ORDER BY Several Columns

Example : SELECT * FROM Customers

ORDER BY Country ASC, CustomerName DESC;

MySQL MIN() and MAX() Functions

 The MIN() function returns the smallest value of the selected column.
 The MAX() function returns the largest value of the selected column.

MIN() Syntax : SELECT MIN(column_name)

FROM table_name

WHERE condition;

MAX() Syntax : SELECT MAX(column_name)

FROM table_name

WHERE condition;
ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 bags 18

2 Chang 1 1 24 - 12 oz bottles 19

3 Aniseed Syrup 1 2 12 - 550 ml bottles 10

4 Chef Anton's Cajun Seasoning 2 2 48 - 6 oz jars 22

5 Chef Anton's Gumbo Mix 2 2 36 boxes 21.35

Example : SELECT MIN(Price) AS SmallestPrice

FROM Products;

SmallestPrice
2.50

Example : SELECT MAX(Price) AS LargestPrice

FROM Products;

LargestPrice
21.35

MySQL COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that matches a specified criterion.

COUNT() Syntax : SELECT COUNT(column_name)

FROM table_name

WHERE condition;

The AVG() function returns the average value of a numeric column.

AVG() Syntax : SELECT AVG(column_name)

FROM table_name

WHERE condition;

The SUM() function returns the total sum of a numeric column.

SUM() Syntax : SELECT SUM(column_name)

FROM table_name

WHERE condition;
Example : SELECT COUNT(ProductID)

FROM Products;

COUNT(ProductID)
15
Note: NULL values are not counted.

Example : SELECT AVG(Price)

FROM Products;

AVG(Price)
18.07
Note: NULL values are ignored.

Example : SELECT SUM(Price)

FROM OrderDetails;

SUM(Price)
90.35
Note: NULL values are ignored.

MySQL LIKE Operator

 The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
 There are two wildcards often used in conjunction with the LIKE operator:
The percent sign (%) represents zero, one, or multiple characters
The underscore sign (_) represents one, single character
The percent sign and the underscore can also be used in combinations!

Syntax : SELECT column1, column2, ...

FROM table_name

WHERE columnN LIKE pattern;

Here are some examples showing different LIKE operators with '%' and '_' wildcards:

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
Finds any values that start with "a" and are at least 2 characters in
WHERE CustomerName LIKE 'a_%'
length
Finds any values that start with "a" and are at least 3 characters in
WHERE CustomerName LIKE 'a__%'
length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

Example : SELECT * FROM Customers

WHERE CustomerName LIKE 'a%';

Example : SELECT * FROM Customers

WHERE CustomerName LIKE '%a';

Example : SELECT * FROM Customers

WHERE CustomerName LIKE '%or%';

Example : SELECT * FROM Customers

WHERE CustomerName LIKE '_r%';

Example : SELECT * FROM Customers

WHERE CustomerName LIKE 'a__%';

Example : SELECT * FROM Customers

WHERE ContactName LIKE 'a%o';

Example : SELECT * FROM Customers

WHERE CustomerName NOT LIKE 'a%';

MySQL IN Operator

 The IN operator allows you to specify multiple values in a WHERE clause.


 The IN operator is a shorthand for multiple OR conditions.

Syntax : SELECT column_name(s)

FROM table_name

WHERE column_name IN (value1, value2, ...);

or:
SELECT column_name(s)

FROM table_name

WHERE column_name IN (SELECT STATEMENT);

Example : SELECT * FROM Customers

WHERE Country IN ('Germany', 'France', 'UK');

Example : SELECT * FROM Customers

WHERE Country NOT IN ('Germany', 'France', 'UK');

Example : SELECT * FROM Customers

WHERE Country IN (SELECT Country FROM Suppliers);

MySQL BETWEEN Operator

 The BETWEEN operator selects values within a given range. The values can be numbers, text,
or dates.
 The BETWEEN operator is inclusive: begin and end values are included.

Syntax : SELECT column_name(s)

FROM table_name

WHERE column_name BETWEEN value1 AND value2;

Example : SELECT * FROM Products

WHERE Price NOT BETWEEN 10 AND 20;

Example : SELECT * FROM Products

WHERE Price BETWEEN 10 AND 20

AND CategoryID NOT IN (1,2,3);

Example : SELECT * FROM Products

WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'

ORDER BY ProductName;

Example : SELECT * FROM Products

WHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef Anton's Cajun Seasoning"

ORDER BY ProductName;
Example : SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;

MySQL Aliases
 Aliases are used to give a table, or a column in a table, a temporary name.
 Aliases are often used to make column names more readable.
 An alias only exists for the duration of that query.
 An alias is created with the AS keyword.

Alias Column Syntax

SELECT column_name AS alias_name


FROM table_name;
Example
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

The following SQL statement creates two aliases, one for the CustomerName column and one for the
ContactName column. Note: Single or double quotation marks are required if the alias name contains
spaces:

Example

SELECT CustomerName AS Customer, ContactName AS "Contact Person"


FROM Customers;

The following SQL statement creates an alias named "Address" that combine four columns (Address,
PostalCode, City and Country):

Example

SELECT CustomerName, CONCAT_WS(', ', Address, PostalCode, City, Country) AS Address


FROM Customers;

Alias for Tables Example

The following SQL statement selects all the orders from the customer with CustomerID=4 (Around the Horn).
We use the "Customers" and "Orders" tables, and give them the table aliases of "c" and "o" respectively
(Here we use aliases to make the SQL shorter):

Example

SELECT o.OrderID, o.OrderDate, c.CustomerName


FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Around the Horn' AND c.CustomerID=o.CustomerID;
The following SQL statement is the same as above, but without aliases:

Example

SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName


FROM Customers, Orders
WHERE Customers.CustomerName='Around the Horn' AND Customers.CustomerID=Orders.CustomerID;

MySQL Joining Tables

 A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.

The SQL INNER JOIN joins two tables based on a common column, and selects records that have matching
values in these columns.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example
SELECT Customers.customer_id, Customers.first_name, Orders.amount
FROM Customers
INNER JOIN Orders
ON Customers.customer_id = Orders.customer;
MySQL LEFT JOIN joins two tables based on a common column, and selects records that have matching
values in these columns and remaining rows from the left table.

The syntax of LEFT JOIN is:

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Example
SELECT Customers.customer_id, Customers.first_name, Orders.amount
FROM Customers
LEFT JOIN Orders
ON Customers.customer_id = Orders.customer;
MySQL RIGHT JOIN joins two tables based on a common column, and selects records that have matching
values in these columns and remaining rows from the right table.

The syntax of RIGHT JOIN is:

SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Example

SELECT Customers.customer_id, Customers.first_name, Orders.amount


FROM Customers
RIGHT JOIN Orders
ON Customers.customer_id = Orders.customer;

MySQL GROUP BY Statement


 The GROUP BY statement groups rows that have the same values into summary rows, like "find the
number of customers in each country".
 The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(),
AVG()) to group the result-set by one or more columns.

GROUP BY Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
MySQL HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate
functions.

HAVING Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

HAVING Example
SELECT City, SUM(Salary) as TotalSalary
FROM Employee
GROUP BY City
HAVING City = 'London';

SELECT City, SUM(Salary) as TotalSalary


FROM Employee
WHERE Gender = 'Male'
GROUP BY City
HAVING City = 'London';

SELECT Department, SUM(Salary) AS Total_Salary


FROM Employee
GROUP BY Department
HAVING SUM(Salary) > 170000;

SELECT Department, COUNT(*) AS 'Number of employees'


FROM Employee
GROUP BY Department
HAVING COUNT(*) > 5;

SELECT Department, AVG(Salary) AS 'Average salary'


FROM Employee
GROUP BY Department
HAVING AVG(Salary) > 50000;

Difference Between Where Clause and Having Clause in MySQL


1. WHERE clause in MySQL cannot be used with aggregate functions whereas HAVING clause can be
used with aggregate functions. That means the WHERE clause in MySQL is used for filtering individual
rows on a table whereas the HAVING clause in MySQL is used to filtering the groups which are
created by the Group by Clause.
2. The WHERE comes before the GROUP BY clause. That means the WHERE clause filters rows before
aggregate calculations are performed. On the other hand, the HAVING clause comes after GROUP by
Clause. That means the HAVING clause filters rows after aggregate calculations are performed. So,
from a performance standpoint, the HAVING Clause is slower than the WHERE Clause and should be
avoided if possible.
3. WHERE and HAVING clauses can be used together in a single SELECT statement. In that case, the
WHERE clause is applied first to filter individual rows. The rows are then grouped and aggregate
calculations are performed, and then only the HAVING clause filters the groups in MySQL.
4. The MySQL WHERE clause can be used with Select, Insert, and Update statements whereas the
HAVING clause can only be used with the Select statement.
5. We can use the Where clause without using the Group by Clause but we can not use the Having
Clause without using the Group by Clause in MySQL.

You might also like