Introduction To Structured Query Language (SQL)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

Introduction to Structured Query

Language (SQL)
SELECT Statement
https://www.youtube.com/watch?v
=pcjXQqKWWNw
Introduction
• SQL to query a database to extract useful
information
• The SELECT statement is used to select
data from a database.
• The data returned is stored in a result
table, called the result-set.

2
SELECT Syntax
SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field


names of the table you want to select data
from.

3
Demo Database
Customer Customer ContactN PostalCo
Address City Country
ID Name ame de
1 Alfreds
Maria Obere
Futterkist Berlin 12209 Germany
Anders Str. 57
e
Ana
Avda. de
Trujillo
Ana la México
2 Empared 05021 Mexico
Trujillo Constituci D.F.
ados y
ón 2222
helados
Antonio
Antonio Matadero México
3 Moreno 05023 Mexico
Moreno s 2312 D.F.
Taquería
4 120
Around Thomas
Hanover London WA1 1DP UK
the Horn Hardy
Sq.
Berglund
Christina Berguvsv
5 s Luleå S-958 22 Sweden
Berglund ägen 8
snabbköp

4
SELECT Column Example
• SELECT CustomerName, City FROM
Customers;

5
SELECT * Example
• SELECT * FROM Customers;

6
SQL 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.
• SELECT DISTINCT column1, column2, ...
FROM table_name;
• Example
• SELECT DISTINCT Country FROM Customers;

7
COUNT with SELECT
• SELECT COUNT(DISTINCT Country)
FROM Customers;

The following SQL statement lists the


number of different (distinct) customer
countries

8
Putting retrieved data in new column
• SELECT OrderID, ProductID, UnitPrice*Quantity AS
"Regular Price", UnitPrice*Quantity-
UnitPrice*Quantity*Discount AS "Price After Discount"
FROM order_details;

• SELECT Count(*) AS DistinctCountries


FROM (SELECT DISTINCT Country FROM Customers);
Result:-

DistinctCountries
21 9
SQL WHERE Clause
• The WHERE clause is used to filter records.
• The WHERE clause is used to extract only those
records that fulfill a specified condition.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
• SELECT * FROM Customers
WHERE Country='Mexico';
• SELECT * FROM Customers
WHERE CustomerID=1; 10
Operators in The WHERE
Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
Not equal. Note: In some versions of
<>
SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
To specify multiple possible values for
IN
a column

11
SQL AND, OR and NOT
Operators
• 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 is 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.
12
AND Syntax
• SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND
condition3 ...;
• Example
SELECT * FROM Customers
WHERE Country='Germany' AND
City='Berlin';

13
OR Syntax
• SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR
condition3 ...;
• Example
• SELECT * FROM Customers
WHERE City='Berlin' OR City='München';

14
NOT Syntax
• SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
• Example
SELECT * FROM Customers
WHERE NOT Country='Germany';

15
Combining AND, OR and NOT

• SELECT * FROM Customers


WHERE Country='Germany' AND
(City='Berlin' OR City='München');
• SELECT * FROM Customers
WHERE NOT Country='Germany' AND
NOT Country='USA';

16
SQL LIKE Operator
• 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 used in conjunction with
the LIKE operator:
• % - The percent sign represents zero, one, or
multiple characters
• _ - The underscore represents a single character

17
LIKE Syntax
• SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Example 1
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
The following SQL statement selects all customers with
a CustomerName starting with "a":
Example 2
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
The following SQL statement selects all customers with
18
a CustomerName ending with "a"
More…
Example 3
• The following SQL statement selects all customers with a
CustomerName that have "or" in any position:
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
Example 4
The following SQL statement selects all customers with a
CustomerName that have "r" in the second position
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';

19
Revision
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' Finds any values that ends with "a"
Finds any values that have "or" in any
WHERE CustomerName LIKE '%or%'
position
Finds any values that have "r" in the
WHERE CustomerName LIKE '_r%'
second position
WHERE CustomerName LIKE Finds any values that starts with "a"
'a_%_%' and are at least 3 characters in length
Finds any values that starts with "a"
WHERE ContactName LIKE 'a%o'
and ends with "o"

20
SQL BETWEEN Operator
• The SQL 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.

21
BETWEEN Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1
AND value2;
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

22
NOT BETWEEN Example
Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;

23
SQL ORDER BY Keyword
• SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example
• SELECT * FROM Customers
ORDER BY Country;
• SELECT * FROM Customers
ORDER BY Country DESC;
• SELECT * FROM Customers
ORDER BY Country ASC, CustomerName
24
DESC;
Practice Sheet
• https://www.w3resource.com/sql-exercises/sql-retrieve-from-table.php

25
Introduction to SQL (continued)
• SQL is relatively easy to learn
• Basic command set has vocabulary of less
than 100 words

26
Introduction to SQL (continued)

27
Introduction to SQL (continued)

28
The Database Model

29
Listing Table Rows
• SELECT
– Used to list contents of table
– Syntax:
• SELECT columnlist
FROM tablename;
• Columnlist represents one or more
attributes, separated by commas
• Asterisk can be used as wildcard
character to list all attributes
30
Aggregate Functions

31
Practice
• https://blog.oureducation.in/sql-questions/
• Q6, Q7, Q8,Q9,

• http://tutorial.techaltum.com/sql-queries-
for-practice.html

32

You might also like