Professional Documents
Culture Documents
Commands
CAUTION!
Use these Notes along with the main notes as well.
Summary of DDL Commands
This query will show, in alphabetical order of second name, the first and
second names of all students in class 7A:
This query will show the teacher’s name and the subject taught:
These statements will delete the specified row(s) from the Student table (take care only: DELETE FROM Student will
delete the whole table!):
Another type of SELECT statement is known as Aggregate Function which can evaluate the one
attribute amongst several records to return one value.
8SELECT COUNT(BandName) Counts the Number of bands (i.e. records in table) using primary key for that table (BandName)
FROM Band;
9On the other hand, aggregate functions require the specification of an attribute. Like in this instruction we
9SELECT AVG(NumberOfMembers) are finding the average number of members of ALL bands using AVG command with NumberOfMembers
FROM Band; specified.
10SELECTSUM(NumberOfMembers) 10Similarily SUM will return the total number of members for all bands.
FROM Band;
Using the “LIKE” keyword
The following SQL selects all customers with a
CustomerName starting with "a":
1SELECT 1As it can be seen the attributes that need to be fetched are mentioned after the SELECT statement and the
BandName FROM Band;
table from which they are to be fetched comes after the FROM keyword.
2SELECT BandName, NumberOfMembers 2We can also retrieve data from multiple fields using comma between attributes.
FROM Band;
3Or we can simply use the “*” symbol to output data from ALL fields from the BAND table.
3SELECT * FROM Band;
4We can also ensure that the results are presented according to some order. In this case the result is to be
4SELECT BandName, NumberOfMembers
FROM Band presented in the order of BandName (i.e. alphabetical order due to BandName being text). There will be no
ORDER BY BandName; duplication of BandName as it is a primary key.
5SELECT BandName 5Inthe Band-Booking Table the attribute BandName may occur many times. To present the names of bands that
FROM Band-Booking have been booked this query can be used. However, the GROUP BY keyword prevents the BandName being
GROUP BY BandName; repeated on output.
6SELECT BandName 7SELECT BandName, NumberOfMembers 7Thisensures that only BandName
6A further extension to the above
FROM Band-Booking query allows us to add a criteria using FROM Band and NumberOfMembers are returned
WHERE Headlining = ‘Y’ the WHERE clause. This query will WHERE NumberOfMembers > 2 from Band table that have more than
GROUP BY BandName; produce a single output for each band ORDER BY BandName; two members.
that has headlined.