SA0951a: Introduction to SQL Structured Query Language Structured Query Language Lots of SQL books in library and ebrary e.g. Head First SQL, SQL for Mere Mortals (on SAFARI) Connolly/Begg (4 th ed.) 5.1, 5.2, 5.3.1 to 5.3.4, 5.3.7 Lots of SQL websites: e.g. http://www.w3schools.com/SQl/default.asp# or http://www.free-ed.net/free-ed/InfoTech/informit/ ITLC15.asp: Parts 3 & 4
21
Embed
Structured Query Language SA0951a: Introduction to SQL Structured Query Language Lots of SQL books in library and ebrary e.g. Head First SQL, SQL for Mere.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SA0951a: Introduction to SQLStructured Query LanguageStructured Query Language
Lots of SQL books in library and ebrarye.g. Head First SQL, SQL for Mere Mortals (on SAFARI)Connolly/Begg (4th ed.) 5.1, 5.2, 5.3.1 to 5.3.4, 5.3.7
Lots of SQL websites: e.g. http://www.w3schools.com/SQl/default.asp# orhttp://www.free-ed.net/free-ed/InfoTech/informit/ITLC15.asp: Parts 3 & 4
2
So what is the most sought after skill in industry today?
A - being able to sort out margins efficiently in Microsoft WORD
B - an ability to throw your PC at your workmate?
C - the ability to send e-mails to the wrong people
D - SQL
3
What is SQL?
A database language 3 parts
DDL (Data Definition Language) set up tables, create keys, change table design lab 1
DCL (Data Control Language)control access permissions to the database etc. later
DML (Data Manipulation Language)query, manipulate data in table(s) now and next
non-procedural i.e. specify what to do, not how to do it
Widespread use in development (embedded in multiple platforms)
SELECT *FROM TransportWHERE Make='BMW' OR Make='VOLVO';
* denotes all columns
Semi-colon at end
How many rows in the answer?
query 1
http://uadisq01.uad.ac.uk:5560/isqlplus
5
Column Alias (AS)
Renames attributes for output result
SELECT salary AS PayFROM Personnel WHERE Surname = 'FRENCH';
Pay20184
RESULT
In Oracle, you can omit AS
You’ll see from your database that French’s salary is 20184
query 2
6
Using two or more tables
Can use many tables E.g. SELECT p.div, b.div, surnameFROM personnel p, branch bWHERE p.div=b.div and City='BRISTOL';
List all tables used in FROM clause Specify matching columns in WHERE clause!!!!!! Make it clear which table each column belongs to
Use table.column notation where ambiguous Can use table aliases to shorten
Table aliases
query 3
7
WHERE Clause
Any Boolean expression involving attribute conditions Use
column names, symbols =, <, etc., calculations, numbers, text Combine conditions with AND, OR Text strings must be enclosed in single quotes
case sensitive (in Oracle)! E.g. this will return nothing from your database
SELECT * FROM PERSONNELWHERE Sex='f';query 4
8
LIKE operator
Used for string comparisons and pattern matching in WHERE clause
Uses wildcards: _ (underscore): any single character (? in Access) % (percent): string of any length (* in Access)
SELECT * FROM PERSONNELWHERE surname LIKE '_A%E'
picks 'BATE' and 'MACRAE' but not 'HAMILTON' or 'RAINES'
query 5
9
ORDER BY
ORDER BY <column> [ASC|DESC] Sorts the result according to the column Can use several levels, e.g.SELECT *FROM PERSONNELORDER BY JobTitle, Salary Desc; Sorts results by jobtitle,
and where jobtitle is the same, sorts by salary, highest first
query 6
SELECT Surname,City,Salary AS IncomeFROM Personnel,Branch BWHERE Personnel.Div = b.div AND (City LIKE '%S%' OR Surname LIKE '_R%')
ORDER BY CITY, SALARY DESC;
What's the result?
SURNAME CITY INCOMEKHAN BRISTOL 42000RAINES BRISTOL 25872 HAMILTON BRISTOL 18534TRINGHAM BRISTOL 9384 FRENCH LONDON 20184BRAY LONDON 18000BROCK LONDON 12288
SURNAME CITY INCOMETRINGHAM BRISTOL 9384
SURNAME CITY INCOMETRINGHAM BRISTOL 9384 FRENCH LONDON 20184BRAY LONDON 18000BROCK LONDON 12288
SURNAME CITY INCOMEKHAN BRISTOL 42000RAINES BRISTOL 25872 HAMILTON BRISTOL 18534TRINGHAM BRISTOL 9384 KUMAR LONDON 30816FRENCH LONDON 20184BRAY LONDON 18000MACRAE LONDON 16200BROCK LONDON 12288
A B
CD
query 7
11
Explicit Join
can specify JOINS explicitly in the From clause different types of JOIN operations: