SQL Notes
SQL Notes
Programming
using SQL:
Review??
SQL:
Structured Query Language
providing controlled
DBMS
access to a
3
4
5
Creating Tables
Create the table.
CREATE TABLE dept
(deptno NUMBER(8),
dname VARCHAR(14),
loc VARCHAR(13));
DBMS 10
Data type
Data Type
Indicates the type of data that can be
represented in the value of the data
element.
Types of Data types that can be used in
SQL.
- int , Char(10), DBMS
Varchar(15), Date. 11
Data Retrieval from one
Table
Four major scenarios of data retrieval
from a single table:
DBMS 12
Retrieving data from a Schema
1. Use of Arithmetic operators… Calculated
Fields, Renaming columns.
2. Comparision Operators
3. Other Comparision Operators( Between,
In ,Like, Is Null, NOT, IS NOT NULL)
4. Logical Conditions ( AND OR NOT)
5. Column Assortment.
6. Aggregate Function .
7. Group By : Collect data from multiple
records and claster the result by one or
more columns
8. Functions DBMS 13
A schema is a collection of logical
structures of data, or schema
objects. A schema is owned by a
database user and has the same
name as that user. Each user owns
a single schema.
DBMS 14
Example 5.1 All
Columns, All Rows
Request: List full details of all staff.
SELECT staffNo, fName, lName,
address,
position, sex, DOB, salary,
branchNo
FROM Staff;
Can use * as an abbreviation for 'all
columns':
SELECT *
FROM Staff;
DBMS 15
Example 5.1 All
Columns, All Rows
DBMS 16
Example 5.2 Specific
Columns, All Rows
Produce a list of salaries for all
staff, showing only staff number,
first and last names, and salary.
DBMS 17
Example 5.2 Specific
Columns, All Rows
DBMS 18
Distinct Key word
Distinct is used to retrieve rows
from a given table that have
unique values for a given column.
For example say we have a table
of employees and in this table of
employees we have several job
titles from janitors to CEOs. We
would like to know just how many
distinct job titles we have.
some of the columns
DBMS
may contain19
Example 5.3 Use of
DISTINCT
List the property numbers of all
properties that have been
viewed.
SELECT propertyNo
FROM Viewing;
DBMS 20
Example 5.3 Use of
DISTINCT
Use DISTINCT to eliminate
duplicates:
DBMS 21
Example 5.4 Calculated Fields
DBMS 22
Example 5.4 Naming
columns
To name column, use AS clause:
SELECT staffNo, fName,
lName, salary/12
AS monthlySalary
FROM Staff;
DBMS 26
DBMS 27
Comparison Operators
= equals
< is less than
> is less than
!= Not Equal
>= greater than or equal to.
<= Less than or equal to
DBMS 28
Example 5.6 Compound
Comparison Search Condition
List addresses of all branch offices in
London or Glasgow.
SELECT *
FROM Branch
WHERE city = 'London' OR city
= 'Glasgow';
DBMS 29
Compound Comparison
Search Condition
List all positions that are
Managers or sex =M.
DBMS 30
Example 5.7 Range
Search Condition
BETWEEN test includes the endpoints of
range.
DBMS 31
Example 5.7 Range Search
Condition
DBMS 32
Example 5.7 Range
Search Condition
Also a negated version, NOT BETWEEN can
be used.
BETWEEN does not add much to SQL's
expressive power Could also write:
QN : Write an SQL
DBMS statement that will
33
IN Clause
IN clause :is a special kind of
operator for use in your where
clauses.
Recall that in a where expression
only one value is allowed to be
sent through the query. With the in
operator one can send multiple
values in the where clause.
DBMS 34
Example 5.8 Set
Membership
List all managers and supervisors.
SELECT staffNo, fName, lName,
position
FROM Staff
WHERE position IN ('Manager',
‘Supervisor');
DBMS 35
Membership
There is a negated version (NOT
IN).
IN does not add much to SQL's
expressive power.
Could have expressed this as:
SELECT staffNo, fName, lName, position
FROM Staff
WHERE position='Manager' OR
position=‘Supervisor';
DBMS 37
Pattern Matching
SQL has two special pattern
matching symbols.
DBMS 39
Example 5.10 NULL
Search Condition
DBMS 40
SQL - Order By
DBMS 41
Single Column Ordering
List salaries for all staff,
arranged in descending order of
salary.
DBMS 42
Example 5.11 Single
Column Ordering
DBMS 43
Example 5.12 Multiple
Column Ordering
Produce abbreviated list of
properties in order of property
type.
DBMS 44
Example 5.12 Multiple
Column Ordering
DBMS 45
Multiple Column
Ordering
Four flats in this list - as no minor sort key
specified, system arranges these rows in
any order it chooses.
To arrange in order of rent, specify minor
order:
DBMS
46
Example 5.12 Multiple
Column Ordering
DBMS 47
SELECT Statement -
Aggregates
AGGREGATE Functions: Used to summarize data
on a query, report, or form
ISO standard defines five aggregate
functions:
DBMS 49
SELECT Statement -
Aggregates
COUNT(*) counts all rows of a table,
regardless of whether nulls or
duplicate values occur.
Can use DISTINCT before column name
to eliminate duplicates.
DISTINCT has no effect with MIN/MAX,
but may have with SUM/AVG.
DBMS 50
Example 5.13 Use of
COUNT(*)
How many properties cost more
than 350 per month to rent?
SELECT COUNT(*) AS count
FROM PropertyForRent
WHERE rent > 350;
DBMS 51
COUNT
Find the number of staff
who belong to
branchno=b005?
SELECT COUNT(*)
FROM staff
WHERE branchno = ‘b005’;
DBMS 52
Example 5.15 Use of COUNT
and SUM
Find number of Managers and sum of their
salaries.
SELECT COUNT(staffNo) AS count,
SUM(salary) AS sum
FROM Staff
WHERE position = 'Manager';
DBMS 53
Example 5.16 Use of MIN, MAX,
AVG
54
Group By
Used to cluster identical data
sets in to a group. It works
hand in hand with the Aggregate
functions .
SQL Statements- Summary
SELECT Data Retrieval
DBMS 56
SQL Statements cont’d
Statement Description
SELECT Retrieves data from the database
61 DBMS
INSERT Syntax
INSERT INTO "table_name"
("column1", "column2", ...)
VALUES ("value1", "value2", ...)
DBMS 62
Inserting Data Into the
Table
The INSERT statement
Select * from user_tables;
DBMS 64
Exercise
Write an SQL statement to create the
following tables
Branch(branchNo, street, city,
postcode)
Staff(staffNo, fName, lName, position,
sex, DOB, salary)
DBMS 65
Exercise
Set up two tables.
Product with columns ProductCode 4 digits,
ProductName 15 characters, ProductPrice
Decimal – 4 digits with two after the decimal
place. The product code is the primary key.
Supplier with columns SupplierId 4 digits,
SName 15 characters. SupplierId is the
primary key.
Find the tables in the catalog.
To find tables in the catalog, you manipulate
the SQLPlus buffer.
Select * from cat
Inserting Data Into the Table
General Syntax:
INSERT INTO TableName [ (columnList) ]
VALUES (dataValueList)
or
INSERT INTO "table_name" ("column1",
"column2", ...)
VALUES ("value1", "value2", ...)
DBMS 67
Example 5.35 INSERT … VALUES
DBMS 69