CSC2243: Databases, Visual
Basic and Software Engineering
By NGIRUWONSANGA Albert
E-mail: [Link]@[Link]
Phone contact: 0788 471 881
University of Rwanda
College of Education-School of Education
Department of Mathematics, Science and Physical Education
Year 2 MCsE DEGREE-Academic Year: 2019/2020
SQL ALTER statement
Changing a table name
• The syntax for changing a table name is as follows:
• alter table table_name rename new_table_name;
2
SQL SELECT statement
3
SQL SELECT statement
select * from table where comb is not null;
USING DISTINCT
• There will be times where your query will contain
redundant data. For instance, if you want to list
combinations from students table. You can use distinct to
avoid repetition of combinations.
select distinct comb, fname, sname from students;
4
SQL SELECT statement
• When you use distinct, the MySQL engine will remove rows
with identical results.
select distinct comb from students;
USING BETWEEN
• You can also choose values within a range by using
the between predicate.
• between works for numeric values as well as dates.
5
SQL SELECT statement
USING BETWEEN
• In the following query, lastchanged is a timestamp
column. If you wanted to find the people who signed
up on the day of March 22, 2019, you could use this
query:
select *from users where lastchanged between
20190322000000 and 20190322235959;
6
SQL SELECT statement
USING BETWEEN
• You can also use between on text strings. If you wished
to list all the last names in the first half of the alphabet,
this query would work.
• Note that the following query will not include names
that start with “m”.
select * from users where lname between ‘a’ and ‘m’;
7
SQL SELECT statement
USING IN/NOT IN
• The in predicate is helpful if there are several possible
values for a single column that can be returned.
• If you queried the students table to get all the
combinations in DMSPE, you could write the query like
this:
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
• So, for example, if you were looking for someone
with the first name of Daniel or Danny or Dan, you
would use the percent sign.
select * from students where fname like ‘Dan%’;
14
SQL SELECT statement
USING LIKE
• However, if for some odd reason you needed to find
all of the people in your database with four-letter.
• first names beginning with the letter J, you’d construct
your query like this: (Note that three underscores follow
the letter J.)
15
SQL SELECT statement
USING LIKE
select * from students where fname like ‘J___’;
• The three underscores will match any characters
and return names like Jean, John, and Jack.
• Jay and Johnny will not be returned.
16
SQL SELECT statement
Using limit
• The limit predicate will restrict the number of rows
returned from your query.
• It allows you to specify both the starting row and the
number of rows you want returned.
17
SQL SELECT statement
Using limit
• To get the first five rows from the table, run the
following query:
select * from students limit 0,5;
• The above cmd, says that from row number 0, count
and display 5 rows.
18
SQL SELECT statement
Using limit
• You probably noticed that the numbering is like arrays—the first
row is row 0.
• To get the second five rows of the table, you’d run the
following:
select * from users limit 5,5;
• limit is particularly useful in situations where you want to restrict
the display on any one page.
19
SQL SELECT statement
group by and aggregate functions
• Remember back to when we were talking about using
select with distinct and
• how that removed rows we didn’t need? With using
group by and its associated aggregate functions, we
can achieve more.
20
SQL SELECT statement
group by and aggregate functions
• Consider this task: you wish to know the number of
entries from each combination in our database (for
example, six from MCsE, seven from MBE, two from FEE,
one from HGE).
• If you did a select distinct Comb from students order
by Comb;
21
SQL SELECT statement
group by and aggregate functions
• You would get a listing of each combination in the
database, but there’s no way to get the numbers.
• As MySQL goes through the table to process the
query, it simply skips over rows that would return
identical values.
22
SQL SELECT statement
group by and aggregate functions
• However, with group by, MySQL creates a temporary
table where it keeps all of the information on the rows
and columns fitting your criteria.
• The goal of our query is to find out the number of
students from each combination that are in students
table.
23
SQL SELECT statement
group by and aggregate functions: count()
• To do that we will use group by with count().
• select comb, count(*) from registration group by
comb;
• select dpt,count(*) from students group by dpt;
• select comb,count(*) from students group by comb;
24
SQL SELECT statement
group by and aggregate functions: sum()
• To get the total of credit units from each
combination, you’d run the following query:
select comb, sum(CU) from students group by comb;
25
SQL SELECT statement
MIN()
• The min() function pulls out the lowest value in each
grouping.
• select min(contribution) from contributions;
MAX()
• As you probably guessed, max() will return the highest
value in a group: 26
SQL SELECT statement
• select max(contribution) from contributions;
AVG()
• avg () returns the average of the group:
• select avg(contribution) from contributions;
select min(DisciplineMarks), max(DisciplineMarks),
avg(DisciplineMarks) from students where Class=3;
27
SQL SELECT statement
We could use all of these together to create the
following query:
select min(DisciplineMarks) as MINIMUM,
max(DisciplineMarks) as MAXIMUM,
avg(DisciplineMarks) as AVERAGE from students
where Class=3;
28