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

Chapter 6: Structured Query Language (SQL) : Customer Custid Custname Occupation

This document discusses Structured Query Language (SQL) and is divided into two main sections - Data Definition Language (DDL) and Data Manipulation Language (DML). DDL includes commands to create, modify and drop tables, while DML allows users to retrieve, insert, update and delete data from tables using commands like SELECT, INSERT, UPDATE and DELETE. It also covers SQL clauses, operators and functions used to query and manipulate data in a database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Chapter 6: Structured Query Language (SQL) : Customer Custid Custname Occupation

This document discusses Structured Query Language (SQL) and is divided into two main sections - Data Definition Language (DDL) and Data Manipulation Language (DML). DDL includes commands to create, modify and drop tables, while DML allows users to retrieve, insert, update and delete data from tables using commands like SELECT, INSERT, UPDATE and DELETE. It also covers SQL clauses, operators and functions used to query and manipulate data in a database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

CHAPTER 6 : STRUCTURED QUERY LANGUAGE (SQL)


a non-procedural language that is, it allows the user to concentrate on specifying what data is
required rather than concentrating on the how to get it.

SQL can be divide into two groups:


1) DDL – Data Definition Language
2) DML – Data Manipulation Language

1. DATA DEFINiTION LANGUAGE (DDL)


Commands used :
a. CREATE TABLE used to create table definitions
b. ALTER TABLE used to modify(add/remove) unwanted attribute(s)/column(s).
c. DROP TABLE used to remove unwanted tables from the database.

a. CREATE TABLES
CREATE TABLE CUSTOMER
( CustID char(4) NOT NULL,
CustName vchar(20),
Occupation vchar (20));

CUSTOMER
CustID CustName Occupation

b. ALTERING TABLES
ALTER TABLE CUSTOMER
ADD (RoomID char(3));

CUSTOMER
CustID CustName Occupation RoomID

c. DROP TABLES
DROP TABLE CUSTOMER;
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

2. DATA MANIPULATION LANGUAGE (DML)

a. SELECT COLUMS (to retrieve rows from tables)

SELECT CustID, CustName, Occupation


FROM CUSTOMER;
Result:
CustID CustName Occupation
C001 Harun Lecturer
C002 Karim Executive
C003 Maria Senior Manager
C004 Jamilah Lecturer
C005 Yusuf Manager

SELECT *
FROM CUSTOMER
Result:
CustID CustName Occupation RoomID
C001 Harun Lecturer 210
C002 Karim Executive 311
C003 Maria Senior Manager 520
C004 Jamilah Lecturer 110
C005 Yusuf Manager 401

b. REMOVING DUPLICATE ROWS

SELECT Occupation
FROM CUSTOMER;
Result:
Occupation
Lecturer
Executive
Senior Manager
Lecturer
Manager

SELECT DISTINCT (Occupation)


FROM CUSTOMER;
Result:
Occupation
Lecturer
Executive
Senior Manager
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

Manager
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

ARITHMETIC EXPRESSIONS
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

SELECT RoomID, RoomType, Price, Price-(Price*5/100)


FROM ROOM;
Result:
RoomID RoomType Price col4
110 Deluxe 210.00 199.50
210 Superior 350.00 332.50
311 Suite 550.00 522.50
401 Deluxe 210.00 199.50
520 Suite 550.00 522.50

RULES!!!

 ()
 * or / are evaluated before + or -
 start from left to right
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

AGGREGATE (Group) FUNCTIONS

 AVG (column-name) returns average value


 COUNT (column-name) returns the number of
 non-null values
 COUNT (DISTINCT column-name) returns the number of distinct values
 COUNT (*) returns the number of rows
 MAX (column-name) returns highest value
 MIN (column-name) returns lowest value
 SUM (column-name) calculates total of value

ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

SELECT AVG (Price)


FROM ROOM;

Result : 374 (210+350+550+210+550)/5

SELECT COUNT (*)


FROM ROOM;

Result: 5
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

COMPARISON OPERATORS

= ---------- equal to
<> / != ---------- not equal to
> ---------- greater than
< ---------- less than
>= ---------- greater than or equal to
<= ---------- less than or equal to
BETWEEN ---------- compares a range of values
IN ---------- tests against values in a test
LIKE ---------- compares a character pattern

a. SPECIFYING SELECTION CONDITION

ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

SELECT RoomID
FROM ROOM
WHERE RoomType=’Deluxe’;

Result:
RoomID
110
401

SELECT RoomID, RoomType


FROM ROOM
WHERE RoomID > 400;

Result:
RoomID RoomType
401 Deluxe
520 Suite
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

b. SELECTING ROWS FROM A RANGE

SELECT RoomID, RoomType, Price


FROM ROOM
WHERE RoomID BETWEEN 200 AND 400;

Result:
RoomID RoomType Price
210 Superior 350.00
311 Suite 550.00

SELECT RoomID, RoomType, Price


FROM ROOM
WHERE RoomID NOT BETWEEN 200 AND 400;
RoomID RoomType Price
110 Deluxe 210.00
401 Deluxe 210.00
520 Suite 550.00

c. MATCHING A VALUE IN A LIST

SELECT RoomType, Price


FROM ROOM
WHERE RoomType IN (‘Deluxe’,’Superior’);

Result:
RoomType Price
Deluxe 210.00
Superior 350.00
Deluxe 210.00

SELECT RoomType, Price


FROM ROOM
WHERE RoomType NOT IN (‘Deluxe’,’Superior’);

Result:
RoomType Price
Suite 550.00
Suite 550.00
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

MATCHING A CHARACTER PATTERN


* ----------------- represents any sequence of zero or more characters
_ (underscore) ----------------- represents any single character

SELECT *
FROM ROOM
WHERE RoomType LIKE ‘*Deluxe*’;
Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
401 Deluxe 210.00 5

SELECT *
FROM ROOM
WHERE RoomType LIKE ‘S*’;
Result:
RoomID RoomType Price Level
210 Superior 350.00 2
311 Suite 550.00 3
520 Suite 550.00 5

SELECT *
FROM ROOM
WHERE RoomType LIKE ‘*e’;
Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

SELECT *
FROM ROOM
WHERE RoomType LIKE ‘S _ _ _ e’;
Result:
RoomID RoomType Price Level
311 Suite 550.00 3
520 Suite 550.00 5
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

COMPOUND EXPRESSIONS WITH BOOLEAN OPERATORS

ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

SELECT RoomID, RoomType


FROM ROOM
WHERE RoomID>=100 AND RoomID<=400;

Result:
RoomID RoomType
110 Deluxe
210 Superior
311 Suite

Logical Operators Precedence

RoomID RoomID>=100 RoomID<=400 AND RESULT


110 T T T selected
210 T T T selected
311 T T T selected
401 T F F not selected
520 T F F not selected

AND OR
OPERATOR OPERATOR
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

SELECT RoomType, RoomID


FROM ROOM
WHERE RoomType=’Deluxe’ OR RoomType=’Superior’;
Result:
RoomType RoomID
Deluxe 110
Superior 210
Deluxe 401

SELECT RoomType, RoomID


FROM ROOM
WHERE RoomID>=100 AND RoomID<=300 OR RoomType LIKE ‘*Deluxe*’;
Results:
RoomType RoomID
Deluxe 110
Superior 210
Deluxe 401

ARITHMETIC EXPRESSIONS
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

SELECT RoomID, RoomType, Level, Price, Price-(Price*5/100)


FROM ROOM
WHERE Level=5 AND (Price-(Price*5/100) > 300;
Result:
RoomID RoomType Level Price col5
520 Suite 5 550.00 522.50
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

ORDERING THE ROWS OF THE RESULT


ACS Ascending (A-Z) or (0-9)
DESC Descending (Z-A) or (9-0)

RoomID RoomType Price Level


110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

a. ORDERING ON A SINGLE COLUMN


SELECT RoomID, RoomType, Level
FROM ROOM
ORDER BY RoomType ACS;
Result:
RoomID RoomType Price
110 Deluxe 210.00
401 Deluxe 210.00
311 Suite 550.00
520 Suite 550.00
210 Superior 350.00

b. ORDERING ON A MULTIPLE COLUMN


SELECT RoomID, RoomType, Level
FROM ROOM
ORDER BY RoomType, RoomID DESC;
Result:
RoomID RoomType Price
401 Deluxe 210.00
110 Deluxe 210.00
520 Suite 550.00
311 Suite 550.00
210 Superior 350.00
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

c. ORDERING ON CALCULATED (Result) COLUMNS


SELECT RoomID, RoomType, Level, Price, Price-(Price*5/100) As DissPrice
FROM ROOM
WHERE Level=5
AND (Price-(Price*5/100) As DissPrice) > 100
ORDER BY DissPrice DESC;

Result:
RoomID RoomType Level Price DissPrice
520 Suite 5 550.00 522.50
401 Deluxe 5 210.00 199.50

GROUPING THE ROWS OF THE RESULT

a. USING THE GROUP BY CLAUSE


CUSTOMER
CustID CustName RoomType RoomID
C001 Harun Deluxe 110
C002 Karim Superior 311
C003 Maria Suite 520
C004 Jamilah Deluxe 110
C005 Yusuf Suite 520

SELECT RoomType, COUNT(RoomID)


FROM CUSTOMER
GROUP BY RoomType;
Result:
RoomType RoomID
Deluxe 2
Superior 1
Suite 2
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

b. JOINING TABLES
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

CUSTOMER
CustID CustName Occupation RoomID
C001 Harun Lecturer 210
C002 Karim Executive 311
C003 Maria Senior Manager 520
C004 Jamilah Lecturer 110
C005 Yusuf Manager 401

SELECT RoomID, CustName, RoomType


FROM ROOM , CUSTOMER
WHERE ROOM.RoomID = CUSTOMER.RoomID ;

Result:

RoomID CustName RoomType


110 Harun Deluxe
210 Karim Superior
311 Maria Suite
401 Jamilah Deluxe
520 Yusuf Suite
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

MODIFYING THE CONTENTS OF DATABASE TABLES

UPDATE ------------------ modifies data in existing rows of a table


INSERT ------------------ inserts new rows into a table
DELETE ------------------ deletes existing rows from a table
ROOM
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

a. UPDATING ROWS
UPDATE ROOM
SET Price=Price+10;

Result:
RoomID RoomType Price Level
110 Deluxe 220.00 1
210 Superior 360.00 2
311 Suite 560.00 3
401 Deluxe 220.00 5
520 Suite 560.00 5

b. INSERTING ROWS

INSERT INTO ROOM


VALUES (‘622’,’Suite’,’600.00’,’6’);

Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5
622 Suite 600.00 6
CHAPTER 6 - STRUCTURED QUERY LANGUAGE (SQL)

c. DELETING ROWS

RoomID RoomType Price Level


110 Deluxe 210.00 1
210 Superior 350.00 2
311 Suite 550.00 3
401 Deluxe 210.00 5
520 Suite 550.00 5

DELETE
FROM ROOM
WHERE RoomType = ‘Suite’;

Result:
RoomID RoomType Price Level
110 Deluxe 210.00 1
210 Superior 350.00 2
401 Deluxe 210.00 5

You might also like