You are on page 1of 14

SQL

DDL & DML


A Definition of SQL

Structured Query Language


Not Programming Language
Data Manipulation Language
(DML)
Data Definition Language (DDL)
Why use It?

Some complex queries cannot


be formulated using QBE
Allows expert Database
Programmers a cleaner, faster
and more precise way of
manipulating data.
Is a generic language provided
with all ODBC databases.
Data Definition Language

Used to create a database and


its components such as tables
and queries
Create Database/Table/User, Drop
Also used to administer the
database through creating
users, setting up views and
granting privileges
Grant, Create View
Data Manipulation Language
SELECT # - chooses a field
* For all fields, separate columns with comma.
You just specify the columns you want data from.
FROM # - a relation (table)
You can have multiple tables as well, separated by comma. But when specifying
fields you have to use table_name.column_name syntax so that you specify
table as well as column to return data from. You can do stuff like join, to join up
related fields like foreign id.
WHERE # - allows condition
Here you can set conditions, You can do stuff like And, or, like and do comparisons
etc.
ORDER BY # - allows sort order, you can specify ASC or DESC
GROUP BY # - allows a grouping total
DISTINCT # - removes duplicates
Distinct will delete duplicate values, so SELECT DISTINCT column_name FROM
table_name

If you include more than one column then itll only choose distinct combinations of all
those columns. So only those that have exact same values in all the listed
columns will be removed.

Then theres update and insert and create table, which alters the table,
Conditions in SQL
Like QBE there are standard conditions used to build SQL Queries:

=,<,>,<>,>=,<=, *
AND
OR
NOT
LIKE
BETWEENAND
IS NULL

So you can do stuff like.


Select stuff from table where column = something AND another_column LIKE
%something%

The % is a wildcard so can be anything.


SQL Example

TblSoftware

Licence_no Customer_ID Package Version Price Service Date of


Agreement Purchase
1000 Renta Payroll 4.0 550 Y 18-2-02

1123 Seymour Accounts 6.2 475 N 1-7-02


2111 Renta Stock 2.0 700 Y 13-7-03

3456 Seymour Stock 2.0 770 Y 6-11-03


4870 Redcabs Payroll 5.0 620 Y 7-7-02

5268 Supag Stock 6.2 900 N 8-12-02

5381 Redcaps Accounts 6.2 520 Y 8-12-02

6001 Pradesh Payroll 5.0 620 Y


7114 Renta Accounts 6.2 500 Y 17-3-02
SQL Queries
SELECT [licence No], [Customer ID],
[Package], [DateofPurchase]
FROM TblSoftware;
SQL Queries

Lets impose some conditions using


the where statement:

SELECT [LicenceNo],[Customer ID],


[ServiceAgreement]
FROM TblSoftware
WHERE [ServiceAgreement]=True;

Should give us..


SQL Queries
SQL Queries

SELECT *
FROM TblSoftware
WHERE [DateofPurchase]
Between #1/1/2000# And
#28/2/2003#
ORDER BY [Customer ID];

You distinguish date with hashes.


SQL Queries
Activity 1

Using Music1.mdb, write SQL queries to


Display all fields, ordered by Album Name
Display the titles of all albums recorded by
Pink Floyd.
Display all the titles released by Nirvana
and the Sex Pistols.
Display any artist who released an album
in 1969.
Display the Title, Artist and Year of all the
Albums released between 1965 and 1975
Activity 2
(Using the table on Heathcoate P310 to help you) and
Countries.mdb, write queries to

Display all countries beginning with the letter U.


Display alphabetically all the countries with a
population over 50 million but less than 100 million.
Display those countries who are UN members,
joined before 1970 and are members of the Security
Council.
Display those countries that are not members of
either the security council or the UN.

You might also like