0% found this document useful (0 votes)
2 views

SQL Class

Sql

Uploaded by

ijeracollins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Class

Sql

Uploaded by

ijeracollins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

SQL:

Structured Query Language

A standard computer database


language for accessing & creating,
manipulating and providing controlled
access to a database.
SQL
SQL is a database language with 3
major components.
 Data Definition Language (DDL) for

defining the different structures


within the database . Create/ modify
 Data Manipulation Language ( DML)

. For managing of database objects.


 Data Control Language: Used for

providing controlled access to a


database.
DBMS 2
Requirements [Very Important]
You will need to install a dbms on your
laptop.
Ideally, database language should allow user
to:
 create the database and relation
structures;
 perform insertion, modification, deletion of
data from relations;
 perform simple and complex queries.

Must perform these tasks with minimal user


effort and command structure/syntax must
be easy to learn
DBMS 3
4
5
Creating Tables
Create the table.
CREATE TABLE dept
(deptno NUMBER(8),
dname VARCHAR(14),
loc VARCHAR(13));

Confirm table creation. using


DESCRIBE dept
6
7
Understanding SQL
Consists of standard English words e.g:

1.CREATE TABLE Staff(staffNo VARCHAR(5),


lName VARCHAR(15), salary int);

2.INSERT INTO Staff VALUES ('SG16',


'Brown', 8300);

3. SELECT staffNo, lName, salary


FROM Staff WHERE salary > 10000;
DBMS 8
Literals
Literals are constants used in SQL
statements.

All non-numeric literals must be


enclosed in single quotes (e.g.
‘Lubaga’).

All numeric literals must not be


enclosed in quotes (e.g. 650.00).
DBMS 9
SELECT Statement
SELECT Specifies which columns are to
appear in output.

FROM Specifies table(s) to be used.

WHERE Filters rows using a given condition.

DBMS 10
Data type

CREATE TABLE Staff(staffNo VARCHAR(5),


lName VARCHAR(15), salary int);

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.

Why Data types are relevant. ???


DBMS 11
Data Retrieval from one Table
Four major scenarios of data retrieval from
a single table:

i. Retrieving all columns and all rows;


ii. Retrieving specific columns and all rows;
iii. Retrieving all columns and specific rows;
iv. Retrieving specific columns and specific
rows.

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.

SELECT staffNo, fName, lName, address,


position, sex, DOB, salary, branchNo
FROM Staff;
Can use * as an abbreviation for 'all columns':
SELECT *
FROM 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.

SELECT staffNo, fName, lName, salary


FROM Staff;

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;

 Comparison operators are used to


define conditions in the where clause
 >, <,>=,<=, =.
DBMS 19
DBMS 20
DBMS 21
Comparison Operators
= equals
< is less than
> is less than
!= Not Equal
>= greater than or equal to.
<= Less than or equal to

DBMS 22
Example 5.7 Range Search
Condition
BETWEEN test includes the endpoints of range.

List all staff with a salary between 20,000 and


30,000.

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary BETWEEN 20000 AND 30000;

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:

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary>=20000 AND salary <= 30000;

Useful, though, for a range of values.

QN : Write an SQL statement that will retrieve all


the fname after SUZAN??
DBMS 24
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 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';

IN is more efficient when set


contains many values.
DBMS 27
Pattern Matching
SQL has two special pattern
matching symbols.

LIKE '%Glasgow%' means a


sequence of characters of any length
containing 'Glasgow'.
Like ‘n%’ ; Starts with letter n.
Like ‘%w’ ; Ends with a letter w.

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

Negated version (IS NOT NULL) can


test for non-null values.

DBMS 30
SQL - Order By

The order by statement allows for table


column assortment. It allows for
ascending or descending lists of your
table column values permitting SQL to
re-order your table rows for the
purpose of viewing.

DBMS 31
Single Column Ordering
List salaries for all staff, arranged in
descending order of salary.

SELECT staffNo, fName, lName,


salary FROM Staff
ORDER BY salary DESC;

DBMS 32

You might also like