Introduction To SQL: Database Languages
Introduction To SQL: Database Languages
INTRODUCTION TO SQL
INTRODUCTION
DATABASE QUERY LANGUAGES
SQL
Databases require two sorts of languages : the data definition
SQL SELECT STATEMENT
language and the data manipulation language.
ORACLE ENHANCEMENTS TO SELECT STATEMENT
CONCLUSIONS The data definition language is used to create databases, tables, to
authorize users etc
SQL IN PERSPECTIVE
> SQL: Structured Query Language. The SQL SELECT statement consists of a core which all
implementations of SQL support:
> Sequel
CUSTOMER
Get the customer details for customer number 12212
place
Case sensitivity
Quoted strings are case sensitive The ORDER BY clause controls the sequence that is used to present the
Q2 SELECT CUST_NUM, CUST_NAME, CUST_ADDRESS results of queries.
FROM CUSTOMER
WHERE CUST_NUM >= 12212; List the customer details ordered by the customer name.
Is the same as:
Q3 select cust_num, cust_name, cust_address Q6 SELECT CUST_NUM, CUST_NAME, CUST_ADDRESS
from customer FROM CUSTOMER
where cust_num >= 12212; ORDER BY CUST_NAME;
But:
Q4 SELECT CUST_NUM, CUST_NAME, CUST_ADDRESS
FROM CUSTOMER CUST_NUM CUST_NAME CUST_ADDRESS
WHERE CUST_ADDRESS = ‘SPINKHILL’; 12212 ESIDRAIN KILAMARSH
Is different from:
SELECT CUST_NUM, CUST_NAME, CUST_ADDRESS 12211 NUGENT SH RENISHAW
Q5
FROM CUSTOMER 46751 QUIKPLUMB SPINKHILL
WHERE CUST_ADDRESS = ‘Spinkhill’;
The ‘GROUP BY’ clause allows tuples to be returned in groups, typically List each date on which payments were made and the total
with some function being applied to the group payment for each of those dates.
CUSTOMER
The ‘HAVING’ clause specifies which groups are returned. Cust_num, Cust_name,Cust_address
ORDER
Q9 SELECT Cust_num, SUM( Value) Order_num, Cust_num, Order_date, Value
FROM order
GROUP BY Cust_num; SELECT Customer.Cust_name, Order_num, Order_date, Value
HAVING SUM (Value) < 2000; Q10
FROM customer, order
WHERE customer.Cust_num = order.Cust_num
Cust_num SUM (VALUE)
46751 1230 CUST_NAME ORDER_NUM ORDER_DATE VALUE
NUGENT SH 1190 24-APR-10 123
NUGENT SH 1296 13-SEP-10 90
NUGENT SH 2343 15-AUG-10 78.9
QIKPLUMB 2131 12-OCT-10 2303
QIKPLUMB 2132 15-NOV-10 1151.1
Additional conditions can be placed in the ‘WHERE’ clause
to further restrict the result.
Queries returning group functions are relationally closed ie. a query such as:
List the details of each invoice item together with the description and
SELECT MAX(Value)
price of the item.
FROM order;
Q14
Select Invoice_No, Part.Part_No, Description, Price, Quant
returns a relation with one tuple in it: MAX(Value)
Q13 From Part, Invoice_Item
1754
Where Part.Part_No = Invoice_item.Part_No;
CONDITIONS IN SQL:
ORACLE ENHANCEMENTS OF THE SELECT STATEMENT
1. ANY SELECT DISTINCT Cust_name
FROM customer, order 1. DISTINCT
Q15
WHERE customer.cust_num = order.cust_num 2. UNION, INTERSECT, MINUS
AND Order_date = ANY ( '13-SEP-10', '15-AUG-10')
SELECT DISTINCT Cust_name, Customer.Cust_num
2. IN SELECT DISTINCT Cust_name FROM customer, order
Q16 FROM customer, order WHERE customer.Cust_num = order.Cust_num
WHERE customer.cust_num = order.cust_num AND order_date >= ‘1-JAN-10’
AND Order_date IN ( '13-SEP-10', '15-AUG-10') INTERSECT
3. BETWEEN SELECT DISTINCT Cust_name Q19 SELECT DISTINCT Cust_name, customer.Cust_num
FROM customer, order FROM customer, order
Q17
WHERE customer.cust_num = order.cust_num WHERE customer.Cust_num = order.Cust_num
AND Order_date BETWEEN ‘24-JUL-10’ AND’30-OCT-10’; AND order_date <= ’31-DEC-10’;
FOR UPDATE
SELECT *
FROM order
WHERE Order_date = ’12-SEP-10’ OUTER JOIN IN ORACLE:
Q21 AND Cust_num= 12211
FOR UPDATE OF Order_number;
SELECT *
ORDER
FROM customer, order
Cno
Order
Date Q22 WHERE customer.Cust_num = order.Cust_num +;
Number
12211 1190 23-4-90
12211 1296 12-9-90
46751 234321 15-8-90 Customer is the dominant table. Null order details will be added to
rows where there are no orders corresponding to a specific customer.
Joining tables together Joining tables together
Putting some of the bits together: Show the name of each person and Putting some of the bits together: Show the name of each person and
how many phone numbers they have. how many phone numbers they have.
SELECT name, COUNT(*) phones
FROM people, phonenumbers
WHERE phonenumbers.id = people.id
GROUP BY name;
SELECT <ATTRIBUTES>
FROM <TABLES>
WHERE <CONDITIONS>
GROUP BY <ATTRIBUTE>
HAVING <CONDITION>
ORDER BY <ATTRIBUTE>