CSC2243-Databases-Part II
CSC2243-Databases-Part II
2
SQL SELECT statement
3
SQL SELECT statement
select * from table where comb is not null;
USING DISTINCT
6
SQL SELECT statement
USING BETWEEN
8
SQL SELECT statement
USING IN/NOT IN
select * from students
where comb= ‘MBE’ or
comb= ‘MCsE’ or
comb = ‘BGE’ or
comb = ‘PCE’ or
comb = ‘PCsE’;
9
SQL SELECT statement
USING IN/NOT IN
• Using in, you can specify a set of possible values
and simplify this statement.
• The following query would achieve the same result:
select * from students where comb in (‘MBE’,
‘MCsE’, ‘BGE’, ‘PCE’, ‘PCsE’);
10
SQL SELECT statement
USING IN/NOT IN
• If you need to achieve the same effect but in reverse,
you can use the not in predicate. To get a listing of all
combinations in the table not belonging in DMSPE,
simple throw in the word ‘not’:
select * from students where comb not in(‘MBE’,
‘MCsE’, ‘BGE’, ‘PCE’, ‘PCsE’);
11
SQL SELECT statement
USING LIKE
• Of course there will be occasions when you are
searching for a string, but you’re not exactly sure what
the string looks like.
• In cases like these, you will need to use wildcard
characters.
• In order to use wildcards, you need the like predicate.
12
SQL SELECT statement
USING LIKE
• There are two wildcard characters available, the
underscore (_) and the percent sign (%).
• The underscore stands for a single character.
• The percent sign represents any number of
characters, including none.
13
SQL SELECT statement
USING LIKE
14
SQL SELECT statement
USING LIKE
15
SQL SELECT statement
USING LIKE
16
SQL SELECT statement
Using limit
17
SQL SELECT statement
Using limit
• To get the first five rows from the table, run the
following query:
• To get the second five rows of the table, you’d run the
following:
20
SQL SELECT statement
group by and aggregate functions
22
SQL SELECT statement
group by and aggregate functions
25
SQL SELECT statement
MIN()
MAX()
28