You are on page 1of 22

SQL – Previous Class

• Create Database,
• Create Table,
• Alter Table,
• Create Table with constraint
• Primary key
• Foreign key
• Date time
• Time stamp
• Insert records
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 Query
If you want to select all the fields
SELECT column1, column2, ... available in the table, use the following
syntax:
FROM table_name;
SELECT * FROM table_name;
Select Query
If you want to select all the fields
SELECT column1, column2, ... available in the table, use the following
syntax:
FROM table_name;
SELECT * FROM table_name;
Select custid, custname
Select * from customer;
from customer;
Select Query – WHERE clause
SELECT column1, column2, ...
The WHERE clause is used to filter FROM table_name
WHERE condition;
records.

It is used to extract only those

records that fulfill a specified

condition.
Select Query – WHERE clause
For integer values…
SELECT * FROM Customer
SELECT * FROM Customer
WHERE custcontry = ‘usa';
WHERE column_name = …. ;
Select Query – WHERE
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:
Select Query – WHERE
The SQL AND, OR and NOT Operators

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.
Select Query – WHERE
The SQL AND Operator

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND
condition3 ...;
Select Query – WHERE
The SQL AND Operator

SELECT * FROM Customer
WHERE custcontry = 'Germany' AND custcity =
'Berlin';
Select Query – WHERE
The SQL OR Operator

SELECT column1, column2, ...


FROM table_name
WHERE condition1 OR condition2 OR
condition3 ...;
Select Query – WHERE
The SQL OR Operator

SELECT * FROM Customer


WHERE custcontry = ‘germany' OR custcontry =
‘usa';
Select Query – WHERE
The SQL NOT Operator

SELECT column1, column2, ...


FROM table_name
WHERE NOT condition;
Select Query – WHERE
The SQL NOT Operator

SELECT * FROM Customer


WHERE NOT custcontry='Germany';
Select Query – Where ( Like )

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
Select Query – WHERE
LIKE

SELECT column1, column2, ...


FROM table_name
WHERE column LIKE pattern;
Select Query – WHERE
LIKE
LIKE Operator Description
WHERE Custname LIKE 'a%' Finds any values that start with "a"
WHERE Custcity LIKE '%a' Finds any values that end with "a"
WHERE Custcountry LIKE '%m%' Finds any values that have "or" in any position

WHERE Custcountry LIKE '_r%' Finds any values that have "r" in the second position

WHERE Custcountry LIKE ‘u_%' Finds any values that start with "a" and are at least 2 characters in length

WHERE Custcountry LIKE ‘u__%' Finds any values that start with "a" and are at least 3 characters in length

WHERE Custname LIKE 'a%o' Finds any values that start with "a" and ends with "o"
Select Query – ORDERBY

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.
Select Query – ORDERBY

SELECT column1, column2, ...


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

Select * from customer order by custid;

Select * from customer order by custname;


Select Query – ORDERBY

SELECT column1, column2, ...


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

Select * from customer order by custname desc;


Select Query – ORDERBY (Multiple
columns)

Select * from customer order by custcountry,


custname;

(First custcountry will be ordered by and then


custname will be ordered by. - like in country
usa, customer name will be arranged.)
Select Query – ORDERBY (Multiple
columns)

Select * from customer order by custcountry asc,


custname desc;

(First custcountry will be ordered by and then


custname will be ordered by. - like in country
usa, customer name will be arranged.)

You might also like