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

SQL STATEMENTS Students

The document describes various SQL statements used to manipulate data in MySQL databases. It covers SQL statements for inserting data, selecting data, filtering with WHERE clauses, using operators like LIKE, IN, AND and OR, sorting with ORDER BY, limiting results with LIMIT, and aggregate functions like COUNT, AVG, MIN, and MAX. Finally, it discusses JOINs to combine data from multiple tables.

Uploaded by

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

SQL STATEMENTS Students

The document describes various SQL statements used to manipulate data in MySQL databases. It covers SQL statements for inserting data, selecting data, filtering with WHERE clauses, using operators like LIKE, IN, AND and OR, sorting with ORDER BY, limiting results with LIMIT, and aggregate functions like COUNT, AVG, MIN, and MAX. Finally, it discusses JOINs to combine data from multiple tables.

Uploaded by

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

SQL STATEMENTS

PHP MySQL Insert Data


The INSERT INTO statement is used to add new records to a MySQL table:

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


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

SQL: INSERT INTO `MyGuests` (`id`, `firstname`, `lastname`, `email`, `reg_date`,


`guest_phone`, `guest_status`, `guest_address`) VALUES (NULL, 'Lisa', 'Bennet',
'[email protected]', current_timestamp(), '555-02933', 'Undecided', '20 Ave, Anytown');

Select and Filter Data From a MySQL


The SELECT statement is used to select data from one or more tables.

SELECT column_name(s) FROM table_name

SELECT * FROM table_name

It returns all the rows and columns of the table

The SELECT * is the shorthand of the SELECT all columns in a table.

Select * from <table>

SQL: SELECT * FROM MyGuests

The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a specified condition.

SELECT column_name(s) FROM table_name WHERE column_name operator value

SQL:
SELECT firstname,lastname, guest_address, guest_status
FROM MyGuests WHERE lastname='Disney';

From MyGuests table, display all columns of all guests who have confirmed before May
30, 2023
SQL: SELECT `id`, `firstname`, `lastname`, `email`, `reg_date`, `guest_phone`,
`guest_status`, `guest_address` FROM `MyGuests` WHERE reg_date<'2023-05-30' and
guest_status='Confirmed'

The SQL 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

LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

SQL: SELECT * FROM `MyGuests` WHERE `lastname` like 'W%';

The SQL AND, OR and NOT Operators


The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:

• The AND operator displays a record if all the conditions separated by AND are TRUE.
• The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

SQL:
→ SELECT `id`, `firstname`, `lastname`, `email`, `reg_date`, `guest_phone`, `guest_status`,
`guest_address` FROM `MyGuests` WHERE `guest_status` = 'Confirmed' AND `guest_
address` LIKE '%Anytown'
→ SELECT * FROM MyGuests WHERE lastname LIKE 'W%' OR lastname LIKE 'D%' OR l
astname LIKE 'G%';
→ SELECT id, firstname, lastname, email, reg_date, guest_phone, guest_status, guest_ad
dress FROM MyGuests WHERE NOT guest_status = 'Confirmed'

The SQL 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.

IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

SQL: SELECT * FROM MyGuests WHERE firstname IN ('Candy', 'Emily', 'Olivia');

The SQL 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.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Create a query to list out registration date, with the most recent first

SQL:
SELECT `id`, `firstname`, `lastname`, `email`, `reg_date`, `guest_phone`, `guest_status`, `guest
_address`
FROM `MyGuests` ORDER BY reg_date DESC;

SQL LIMIT
The SELECT limit clause is used to specify the number of records to return.

SQL:
SELECT * FROM MyGuests LIMIT 5;
SELECT * FROM MyGuests WHERE reg_date>'2023-05-27' LIMIT 5;

The SQL 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;
MIN() Example
The following SQL statement finds the price of the cheapest product:

SQL:
SELECT MIN(Price) AS SmallestPrice
FROM Products;

MAX() Example
The following SQL statement finds the price of the most expensive product:

SQL:
SELECT MAX(Price) AS LargestPrice
FROM Products;

The SQL 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;

COUNT() Example
The following SQL statement finds the number of products:

SQL:
SELECT COUNT(ProductID)
FROM Products;

AVG() Example
The following SQL statement finds the average price of all products:

SQL:
SELECT AVG(Price)
FROM Products;

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

Let's look at a selection from the "Orders" table:


OrderID CustomerID OrderDate

10308 2 1996-09-18

10309 37 1996-09-19

10310 77 1996-09-20

Then, look at a selection from the "Customers" table:

CustomerID CustomerName ContactName Country

1 Alfreds Futterkiste Maria Anders Germany

2 Ana Trujillo Emparedados y Ana Trujillo Mexico


helados

3 Antonio Moreno Taquería Antonio Moreno Mexico

Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the
"Customers" table. The relationship between the two tables above is the "CustomerID"
column.

Then, we can create the following SQL statement (that contains an INNER JOIN), that selects
records that have matching values in both tables:

SQL:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

and it will produce something like this:

OrderID CustomerName OrderDate


10308 Ana Trujillo Emparedados y helados 9/18/1996
10365 Antonio Moreno Taquería 11/27/1996
10383 Around the Horn 12/16/1996
10355 Around the Horn 11/15/1996
10278 Berglunds snabbköp 8/12/1996

You might also like