C2TC Training
Presentation on SQL
What is SQL ?
SQL is a Structured Query Language used
for storing, manipulating and retrieving data
stored in relational database.
SELECT Statement
SELECT Statement or clause is used to retrieve certain records from table.
Syntax : Example:
SELECT column1,column2,… SELECT first_name , last_name
FROM table_name; FROM Customers;
Table: Customers Output
customer_id first_name last_name age country first_name last_name
1 John Doe 31 USA John Doe
2 Robert Luna 22 USA Robert Luna
3 David Robinson 22 UK David Robinson
4 John Reinhardt 25 UK John Reinhardt
5 Betty Doe 28 UAE Betty Doe
WHERE Clause
WHERE clause is used to fetch records from that match specified condition.
Syntax : Example:
SELECT column1,column2,… SELECT *
FROM Customers
FROM table_name
WHERE last_name = ‘Doe’;
WHERE condition;
Output
customer_id first_name last_name age country
1 John Doe 31 USA
5 Betty Doe 28 UAE
AND Operator
AND operator selects data if all conditions are true.
Syntax : Example:
SELECT column1,column2,…
SELECT first_name , last_name
FROM table_name
FROM Customers
WHERE country=‘USA’ AND
WHERE condn1 AND condn2;
last_name=‘Doe’;
Table: Customers
customer_id first_name last_nam age country
e Output
1 John Doe 31 USA
first_name last_name
2 Robert Luna 22 USA
John Doe
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE
OR Operator
OR operator selects data if any one condition is true.
Syntax : Example:
SELECT column1,column2,…
SELECT first_name , last_name
FROM table_name
FROM Customers
WHERE country=‘USA’ OR
WHERE condn1 OR condn2;
last_name=‘Doe’;
Table: Customers
customer_id first_name last_nam age country
e Output
1 John Doe 31 USA
first_name last_name
2 Robert Luna 22 USA
John Doe
3 David Robinson 22 UK
Robert Luna
4 John Reinhardt 25 UK
Betty Doe
5 Betty Doe 28 UAE
NOT Operator
NOT operator selects data if the given condition is false.
Syntax : Example:
SELECT column1,column2,…
SELECT first_name , last_name
FROM Customers
FROM table_name
WHERE NOT country=‘USA’ ;
WHERE NOT condition;
Table: Customers
customer_id first_name last_nam age country
e Output
1 John Doe 31 USA
first_name last_name
2 Robert Luna 22 USA
David Robinson
3 David Robinson 22 UK
John Reinhardt
4 John Reinhardt 25 UK
Betty Doe
5 Betty Doe 28 UAE
IN Operator
IN operator is used with the WHERE clause to match value in the list.
Syntax : Example:
SELECT column1,column2,… SELECT first_name, country
FROM table_name FROM Customers
WHERE columnN IN (list of values); WHERE country IN (‘USA’, ‘UK’);
Table: Customers
customer_id first_name last_nam age country Output
e first_name country
1 John Doe 31 USA
John USA
2 Robert Luna 22 USA
Robert USA
3 David Robinson 22 UK
David UK
4 John Reinhardt 25 UK
John UK
5 Betty Doe 28 UAE
BETWEEN Operator
BETWEEN operator is used with WHERE clause to match value in the range.
Syntax : Example:
SELECT column1,column2,… SELECT first_name, age
FROM table_name FROM Customers
WHERE cN BETWEEN v1 AND v2; WHERE age BETWEEN 20 AND
30;
Table: Customers
customer_id first_name last_nam age country Output
e first_name age
1 John Doe 31 USA
Robert 22
2 Robert Luna 22 USA
David 22
3 David Robinson 22 UK
John 25
4 John Reinhardt 25 UK
Betty 28
5 Betty Doe 28 UAE
LIKE Operator
LIKE operator is used with Wildcards(% ,_) to search for a specified pattern in
column /string.
Example:
Syntax :
SELECT *
SELECT column1,column2,…
FROM Customers
FROM table_name
WHERE last_name LIKE ‘R%’;
WHERE columnN LIKE pattern;
--WHERE last_name LIKE ‘R_’;
Output1
customer_id first_name last_name age country
3 David Robinson 22 UK
4 John Reinhardt 25 UK
Output2
No result /data found
REGEXP Operator
REGEXP operator is used for pattern matching operation based on regular
expressions.
Syntax : Example:
SELECT column1,column2,… SELECT *
FROM table_name FROM Customers
WHERE REGEXP ‘pattern’; WHERE last_name REGEXP ‘^[DL]’ ;
Pattern What pattern matches
Output
^ Beginning of string
customer first_ last_ ag count
$ End of string _id name name e ry
. Any single character 1 John Doe 31 USA
[…] Any character listed
2 Robert Luna 22 USA
between square brackets
5 Betty Doe 28 UAE
[^…] Any character not listed
b/w square brackets
IS NULL Operator
IS NULL operator is used to test for NULL value.
Example:
Syntax :
SELECT *
SELECT column1,column2,…
FROM Customers
FROM table_name
WHERE age IS NOT NULL;
WHERE columnN IS NULL;
--WHERE age IS NULL;
Output1 Output2
customer_id first_name last_nam age country
e No result
1 John Doe 31 USA
2 Robert Luna 22 USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE
ORDER BY clause
ORDER BY clause is to sort the result set in either ascending(ASC) or
descending(DESC) order.
Example:
Syntax : SELECT customer_id, first_name, age
SELECT column1,column2,…
FROM Customers
FROM table_name
ORDER BY first_name;
ORDER BY columnN;
--ORDER BY first_name DESC, age;
Output1 Output2
customer_id first_name age customer_id first_name age
5 Betty 28 2 Robert 22
3 David 22 4 John 25
1 John 31 1 John 31
4 John 25 3 David 22
2 Robert 22 5 Betty 28
LIMIT Operator
LIMIT operator is used to select a fixed number of rows from a database.
Syntax : Example:
SELECT column1,column2,… SELECT first_name, country
FROM Customers
FROM table_name
LIMIT 2;
LIMIT 2;
Table: Customers
Output
customer_id first_name last_nam age country
e
first_name country
1 John Doe 31 USA
John USA
2 Robert Luna 22 USA
Robert USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE
THANK YOU