SQL STATEMENTS Students
SQL STATEMENTS Students
The WHERE clause is used to extract only those records that fulfill a specified condition.
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'
There are two wildcards often used in conjunction with the LIKE operator:
LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
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.
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'
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
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 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;
COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG() Syntax
SELECT AVG(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.
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
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;