SQL Class
SQL Class
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), Varchar(15), Date.
DBMS 12
Data Retrieval/ Class Activity
1.DISTINCT
2.Calculated Fields, Renaming columns.
3.Comparision Operators
4. Other Comparision Operators(
Between, In ,Like, Is Null)
5. Logical Conditions ( AND OR NOT)
6. Column Assortment.
7. Aggregate Function
DBMS 13
Example 5.1 All Columns,
All Rows
Request: List full details of all staff.
DBMS 14
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 15
Example 5.2 Specific
Columns, All Rows
DBMS 16
Distinct Key word
Distinct is used to retrieve rows of your
database 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 may contain
duplicate values. DBMS 17
Example 5.3 Use of
DISTINCT
Use DISTINCT to eliminate
duplicates:
SELECT DISTINCT propertyNo
FROM Viewing;
DBMS 18
Comparison Search Condition
This is done using the WHERE clause.
General syntax:
◦ SELECT clause
FROM table
WHERE condition;
DBMS 22
Example 5.7 Range Search
Condition
BETWEEN test includes the endpoints of range.
DBMS 23
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:
DBMS 25
Example 5.8 Set Membership
List all managers and supervisors.
SELECT staffNo, fName, lName, position
FROM Staff
WHERE position IN ('Manager',
‘Supervisor');
DBMS 26
Example 5.8 Set 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 28
NULL Search Condition
List details of all viewings on property PG4 where
a comment has not been supplied.
Null represents a value for an attribute that is
currently unknown or not applicable for a tuple.
Have to test for null explicitly using special
keyword IS NULL:
SELECT clientNo, viewDate
FROM Viewing
WHERE propertyNo = 'PG4' AND
comment IS NULL;
DBMS 29
Example 5.10 NULL Search
Condition
DBMS 30
SQL - Order By
DBMS 31
Single Column Ordering
List salaries for all staff, arranged in
descending order of salary.
DBMS 32