© Belgium Campus 2021

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

1

© BELGIUM CAMPUS 2021


2

© BELGIUM CAMPUS 2021


3

List of columns that will be


SELECT List of columns that will be displayed
displayed

FROM The table where


The table where the
thecolumns
columns can
can be
befound
found

The different
components of a select WHERE Criteria that will limit the number of rows displayed
statement

For an aggregate function, all columns outside the function must be specified
GROUP BY For an aggregate function, all columns outside the function must be specified here.
here.

HAVING These criteria are


These criteria arespecifically
specifically for
forthe
theaggregate
aggregate function.

ORDER BY To specifythe
To specify thesort
sortorder
orderofofthethe results
results
4

Say we don’t want to see headings like StudentName, StudentAge,


StudentPhone. We can make them a bit more user-friendly.

SELECT StudentName AS ‘The Students Name’ , StudentAge AS ‘The Age of the Student’ ,

StudentPhone AS ‘Number to Contact the student on’

FROM StudentTable
5

How do we ensure we
do not get duplicates?

THEY COME FROM HERE


SELECT province AS ‘THEY COME FROM HERE’
FROM TableStudent Northwest

Gauteng

Gauteng
RESULT
Gauteng

Northern Cape

KwaZulu-Natal

Gauteng

Northwest
6

THEY COME FROM HERE


SELECT DISTINCT province AS ‘THEY COME
FROM HERE’ Northwest
FROM TableStudent Gauteng

Northern Cape
RESULT
KwaZulu-Natal
7

The TOP predicate returns the specified number


of records from the top of the specified table.

This example returns the first 3 THEY COME FROM HERE


records from the 'TableStudent' table.
Northwest

SELECT TOP 3 province AS ‘THEY COME FROM Gauteng


HERE’ Gauteng
FROM TableStudent RESULT
8

SMALLER THAN GREATER THAN EQUAL TO

You don’t usually want to see


all the rows from a table.
There are often certain
criteria to which rows you
want to display. This is what
the WHERE section of a
select statement is for. SMALLER THAN GREATER THAN NOT EQUAL TO
OR EQUAL TO OR EQUAL TO

NOT SMALLER NOT GREATER


THAN THAN
9

We don’t usually want to see everything. In this case the WHERE


clause comes in handy.

SELECT StudentName , StudentPhone , StudentAge

FROM StudentTable

We will not see ALL the students. We will only


WHERE StudentAge > 18 see students who are older than 18.
10

What if we have more than one criterion?

SELECT StudentName , StudentPhone , StudentAge

FROM StudentTable

We will see all the female


WHERE StudentAge > 18 AND StudentGender = ‘Female’ students who are older than 18.
11

What if we have more than one criterion?

SELECT StudentName , StudentPhone , StudentAge

FROM StudentTable
We will see all
female students.

WHERE StudentAge > 18 OR StudentGender = ‘Female’

We will see all


students older than 18.
12

What if we are looking for a range of values?

SELECT * SELECT *

FROM TITLES FROM TITLES

WHERE price BETWEEN 5.00 AND 20.00 WHERE price NOT BETWEEN 5.00 AND 20.00
13

What if we are looking for nothing?

SELECT * SELECT *

FROM TITLES FROM TITLES

WHERE price IS NULL WHERE price IS NOT NULL


14

Who is Sarah, John, and Mary?

SELECT * SELECT *

FROM Customers FROM Customers

WHERE CustomerName IN (‘Sarah’,’John’,’Mary’) WHERE CustomerName NOT IN (‘Sarah’,’John’,’Mary’)


15

We don’t know what we are looking for.

This is an
% _ underscore.

Any string of zero Any single


or more characters character
SELECT *

FROM tableStudents

WHERE Name Like ‘N%’

This SELECT statement will select all the


students whose name starts with N
16

SELECT FirstName , LastName , Age

FROM tableStudent

WHERE Age > 20

ORDER BY LastName asc


17

SELECT FirstName , LastName , Age

FROM tableStudent

WHERE Age > 20

ORDER BY LastName, FirstName asc


18

You now have 20 Minutes to


complete Retrieving Data Exercise 1.

© BELGIUM CAMPUS 2021

You might also like