SQL Overview
• Text-based query language developed by IBM for an experimental RDBMS called System R– originally called SEQUEL
• Most widely supported query language among commercial RDBMS products– although no one supports the standard exactly
• Uses the more informal terms table, row, and column in place of relation, tuple, and attribute
• Declarative, not procedural
• Large, comprehensive database language– reference document is over 600 pages long
Slide 2©SoftMoore Consulting
ANSI/ISO Standardization
Slide 3©SoftMoore Consulting
Year Name Comment
1986 SQL-86 Initial standard
1989 SQL-89 Minor revision
1992SQL-92
(a.k.a. SQL2)Major revision (ISO 9075)
1999SQL-1999
(a.k.a. SQL3)
Added regular expression matching, recursive queries, triggers, non-scalar types, and some object-oriented features
2003 SQL-2003Introduced XML-related features and auto-generated column values
2006 SQL-2006Defines ways of importing and storing XML data in an SQL database
2008 SQL-2008Adds INSTEAD OF triggers. and the TRUNCATE statement
SQL Data Types
• Boolean and Bit String Types– boolean (optional) : can be true, false, or null– bit(n) : n-length bit string (exactly n binary bits)– bit varying(n), varbit(n) : variable n-length bit string (up to n
binary bits)
• Character types– character (n), char(n) : fixed n-length character string– character varying(n), varchar(n) : variable length character string
of up to n characters– national character(n) or nchar(n) : fixed n-length string supporting
an international character set– national character varying(n) or nvarchar(n) : variable length
string supporting an international character set
Slide 4©SoftMoore Consulting
SQL Data Types
• Numeric types– smallint : signed 2-byte integer– integer, int : signed 4-byte integer– real, float : 4-byte floating-point number– double precision : 8-byte floating-point number– numeric(p, s), decimal(p, s) : an exact numeric type with arbitrary
precision p and scale s
• Date and time types– date : calendar date (day, month, and year)– time : time of day– timestamp (includes time zone) : both the date and time– interval : an arbitrarily specified length of time
Slide 5©SoftMoore Consulting
SQL Data Types
• Large Objects*– BLOB (binary large object): variable sized binary object with no
size constraint (maximum size is 2 billion bytes)– CLOB (character large object) : string of variable length without a
size constraint (maximum size is 1 billion characters)
Slide 6©SoftMoore Consulting
*Technically not part of the SQL standard but widelysupported by most commercial database systems.
Using SQL Data Typesin the Real World
Warning: The actual data types and type names differ greatly among the individual RDBMS implementations.
Examples
Slide 7©SoftMoore Consulting
Data Type SQL Standard Oracle MS Access
DECIMAL yes no no
CURRENCY no no yes
NUMBER no yes no
Writing SQL Statements
• Free format
• SQL commands not case sensitive – string literals are
• Commas separate items in a list
• Period separates table name and column name
• Comment lines begin with double dash (--)
• Single quotes around string literals– Access also allows double quotes
• Semicolon marks the end of a statement
• String concatenation uses |– sometimes shows up as ¦
Slide 8©SoftMoore Consulting
Retrieving Data from Tables:The SELECT Statement
• SQL uses the SELECT statement (with many options and variations) as the primary statement for data retrieval.
• Basic format:select <columns> from <tables> where <conditions> order by <sort columns>;
• Exampleselect name, phone from customer;
select name, phone from customer where area_code = '843';
Slide 10©SoftMoore Consulting
Duplicates
• By default, SQL does not eliminate duplicate rows from the result of a SELECT statement.
• The user may explicitly request that duplicate rows be eliminated by using the keyword DISTINCT as part of the request. select distinct state from customer;
Slide 11©SoftMoore Consulting
Conditions
Conditions are specified within the WHERE clause of a SELECT statement.
select * from orders where total_invoice > 1000;
Note: “*” selects all columns.
Slide 12©SoftMoore Consulting
Operators Used in Conditions
• Parentheses for grouping
• Relational Operators = <> or != < > <= >=
• Logical Operators AND OR NOT
• Test Operators IN BETWEEN IS NULL IS NOT NULL
• Pattern Matching OperatorsLIKE (plus a pattern)
_ matches single character (? for Access)% matches multiple characters (* for Access)
Slide 13©SoftMoore Consulting
Examples: Queries on a Single Table
select order_num, sale_date, ship_date, total_invoice from orders where total_invoice > 1000 order by total_invoice desc;
select order_num, cust_num, total_invoice, amount_paid from orders where amount_paid < total_invoice and total_invoice between 1000 and 2000;
select * from customer where last_name like 'M%' order by last_name;
Slide 14©SoftMoore Consulting
Compound Conditions
The boolean operators AND and OR can be used to create compound conditions. Parentheses can be used for grouping
select order_num, cust_num, ship_date, total_invoice from orders where (total_invoice > 1000 and amount_paid < 100) or amount_paid < 1;
Slide 15©SoftMoore Consulting
Set Operators on Tables
• Set union – UNION
• Set difference – EXCEPT
• Set intersection – INTERSECT
• Example:(select name from customer where state = 'FL') union(select name from vendor where state = 'FL');
Slide 16©SoftMoore Consulting
Note: The output of an SQL queryis another table (closure property).
Joining Two Tables
• The join operation can be specified in terms of SELECT, FROM, and WHERE clauses. Attributes from different tables can be qualified by the table name.
• Example select customer.name, customer.phone, orders.total_invoice, orders.amount_paid from customer, orders where customer.cust_num = orders.cust_num and orders.amount_paid < orders.total_invoice;
Alternate format using a table alias: select c.name, c.phone, o.total_invoice, o.amount_paid
from customer c, orders o where c.cust_num = o.cust_num and o.amount_paid < o.total_invoice;
Slide 17©SoftMoore Consulting
Comments on the Join Example
• The first condition in the where clause specifies the “join” condition.
• Table name qualification of attributes can be omitted when there is no ambiguity.
Slide 18©SoftMoore Consulting
Alternate Syntax for Join
A join could be specified more simply using the explicit INNER JOIN (keyword INNER can be omitted) or the NATURAL JOIN operator, which was added in SQL/92.
select c.name, c.phone, o.total_invoice, o.amount_paid from customer c join orders o on c.cust_num = o.cust_num where o.amount_paid < o.total_invoice;
select c.name, c.phone, o.total_invoice, o.amount_paid from customer c natural join orders o where o.total_amount_paid < o.total_invoice;
Slide 19©SoftMoore Consulting
More Complicated Queries
• Complicated, multi-relation queries often involve combinations of tables, columns, and conditions. select c.name, c.phone from customer c, orders o, lineitem li where c.cust_num = o.cust_num and o.order_num = li.order_num and li.quantity > 5;
• Alternatively, we could express this query using the NATURAL JOIN operator. select c.name, c.phone from customer c natural join orders o natural join lineitem li where li.quantity > 5;
Slide 20©SoftMoore Consulting
Inner Joins
• When querying data from two or more tables using joins, most of the time you will want to use an inner join. All examples thus far have used inner joins.
• Example (inner join)select p.name, b.title from publisher p join book b on p.publisher_id = b.publisher_id;
• An inner join will include rows from a table only if there are matching rows in the joined table. In the above example, publishers will not be included if there are no books published by them in the book table.
Slide 21©SoftMoore Consulting
Outer Joins
• To include all rows in a table whether or not there are matching rows in the other table, use an outer join.
• Exampleselect p.name, b.title from publisher p left outer join book b on p.publisher_id = b.publisher_id;
(Note: The word “outer” can be omitted from this query.)
• The above example will select all publishers, even if there are no corresponding books in the book table. Values for b.title will be null for publishers without corresponding books.
Slide 22©SoftMoore Consulting
Outer Joins(continued)
• Outer joins can be “left”, “right”, or “full”, where “full” includes values from both tables.
• Some database systems do not support full outer joins directly, but full outer joins can be simulated by using left and right outer joins and unions.select p.name, b.title from publisher p left join book b on p.publisher_id = b.publisher_idunionselect p.name, b.title from publisher p right join book b on p.publisher_id = b.publisher_id;
Slide 23©SoftMoore Consulting
Subqueries
• A subquery can be used to select data involving two or more tables, but … the selected data can come only from the top-level table.
• Exampleselect name from customer where cust_id in (select cust_id from orders where total_invoice > 1000);
Slide 24©SoftMoore Consulting
Correlated Subqueries
• A correlated subquery uses the same table in both levels of the query. The inner query is recomputed for each row in the outer query.
• Exampleselect b1.title from book b1 where b1.title in (select b2.title from book b2 where b1.title = b2.title and b1.book_id <> b2.book_id);
Finds different books (different IDs) with the same title.
Slide 25©SoftMoore Consulting
EXISTS and NOT EXISTS
• EXISTS and NOT EXISTS are boolean operators that can be used in the “where” clause of correlated subqueries.
• General format:select columns from tables where exists (subquery);
• An EXISTS condition is true if any row in the subquery meets the specified conditions.
• A NOT EXISTS condition is true only if all rows in the subquery do not meet the specified condition/
Slide 26©SoftMoore Consulting
Example: Using the EXISTS Operator
Find the names of all publishers that publish a book on Java (i.e., has the word “Java” in the title).select name from publisher where exists (select * from book where book.publisher_id = publisher.publisher_id and book.title like '%C++%');
This query is equivalent to the following:select name from publisher where publisher_id in (select publisher_id from book where book.title like '%Java%');
Slide 27©SoftMoore Consulting
Derived (Computed) Attributes
• Queries can be used to compute derived attributes. select order_num, cust_num, sale_date, ship_date, total_invoice, amount_paid, total_invoice - amount_paid as balance_due from orders;
• Derived attributes are often computed as part of a view. create view extended_orders as select order_num, cust_num, sale_date, ship_date, total_invoice, amount_paid, total_invoice - amount_paid as balance_due from orders;
Slide 28©SoftMoore Consulting
Update Statements
• INSERT INTO – inserts rows
• DELETE FROM – deletes rows
• UPDATE – modifies attribute values
Slide 30©SoftMoore Consulting
Adding Rows to a Table
-- insert a single row, all columnsinsert into customervalues (6159, 'George W. Bush', '1600 Pennsylvania Avenue', 'Washington', 'DC', '20000', '202-121-8000')
-- insert a single row, selected columns-- (uses defaults/nulls)insert into customer (cust_num, name, phone)values (8152, 'John Smith', '703-924-4352')
-- insert multiple rows with a queryinsert into customer (cust_num, name, phone) select cust_num, name, phone from customer_temp; where state = 'SC';
Slide 31©SoftMoore Consulting
Deleting Rows from a Table
delete from vendor where vendor_num in (2190, 5001, 5002);
Slide 32©SoftMoore Consulting
Note: If you omit the “where” clause,you will delete all rows in the table!
Modifying Data in a Table
-- give everyone in IT a 6% raise update employee set salary = salary*1.06 where dept = 'IT';
Modification can also be accomplished through updateable views.– one table– subset of columns and rows– no summarizing or condensing
Slide 33©SoftMoore Consulting
Data Definition in SQL
• CREATE– schema – domain– table – view– index
• DROP– schema – domain– table – view– index
• ALTER (restructure)– schema – domain– table – view– index
Slide 36©SoftMoore Consulting
Creating a Table: Example 1
create table customer ( cust_num integer not null, last_name varchar(25) not null, first_name varchar(20), company_name varchar(40), email_addr varchar(40) not null,
constraint customer_pk primary key (cust_num), constraint customer_ak unique(email_addr));
Slide 37©SoftMoore Consulting
Creating a Table: Example 2
create table orders ( order_num integer not null, cust_num integer, sale_date date not null, ship_date date, total_invoice decimal(10,2), amount_paid decimal(10,2),
constraint orders_pk primary key (order_num), constraint customer_fk foreign key (cust_num) references customer(cust_num) on delete cascade );
Slide 38©SoftMoore Consulting
Creating a Table from a Query
create table sales_personel as select employee_num, name, phone_num from employee where dept = 'SALES';
Slide 39©SoftMoore Consulting
Creating a View
• Views are created using queriescreate view sales_personel as select employee_num, name, phone_num from employee where dept = 'SALES';
• Access does not support views directly, but it allows stored queries which accomplish similar goals.
Slide 40©SoftMoore Consulting
Creating Indexes and Domains
• Creating an indexcreate index cust_name_index on customer.last_name;
Note: Database systems usually generate “backing indexes” automatically for primary key, foreign key, and unique constraints.
• Creating a domaincreate domain ssn_type as char(9);
Note: The ability to create user-defined types (domains) is not available in some database systems.
Slide 41©SoftMoore Consulting
Dropping Database Objects
drop table employee;
drop view sales_personel;
drop index cust_name_index;
Slide 42©SoftMoore Consulting
Adding Constraints to a Table
-- adding a primary keyalter table departments add constraint pk_departments primary key(dept_num);
-- adding a foreign keyalter table employee add constraint fk_emp_dept foreign key (dept_num) references departments(dept_num) on delete set null on update cascade;
Slide 43©SoftMoore Consulting
Adding and Dropping Columns
• Adding a column to a tablealter table orders add payment_method char(10);
Note: Access requires “ADD COLUMN”, but the keyword “COLUMN” can usually be omitted for other database systems.
• Dropping a column from a table (not available in all database systems)
alter table orders drop column payment_method;
• To drop a foreign key column, the foreign key constraint must be dropped first.
Slide 44©SoftMoore Consulting
The Data Dictionary
• Oracle has a Data Dictionary that maintains all information about the structure of the database (metadata)
• The Data Dictionary is itself a set of tables.– USER_TABLES – ALL_TABLES– USER_VIEWS – ALL_VIEWS– USER_TAB_COLUMNS – ALL_TAB_COLUMNS
• Access does not provide a way to query the data dictionary but provides similar information through a GUI.
Slide 45©SoftMoore Consulting
Obtaining Metadata(Oracle Only)
-- find the names of all of your tablesselect table_name from user_tables;
-- find information about columnsselect column_name, data_type, data_length from user_tab_columns where table_name = 'CUSTOMER';
-- find all constraints (e.g., keys) on the ORDERS tableselect * from user_cons_columns where table_name = 'ORDERS';
Slide 46©SoftMoore Consulting
Note: Similar capabilities exist in other database systems.Example (Derby): select tablename from sys.systables;
Row Functions and Operators
Row functions and operators can be used to calculate new values as part of a query
select cust_num, name, phone from customer where credit_limit + 1000 > 20000;
Slide 48©SoftMoore Consulting
Sample Row Functions and Operators
• Numeric + - * / SQRT (SQR) ABS FLOOR (INT)
• Text || (&) SUBSTR (MID) UPPER (UCASE) LENGTH (LEN) INITCAP (STRCONV(x, 3))
• Date + number - (DATEDIFF) MONTHS_BETWEEN (DATEDIFF)
Slide 49©SoftMoore Consulting
Note: Values in parentheses are those for Access.
Example Using Row Functions
-- find all employees who have been with the-- company at least one yearselect name, hire_date from employee where trunc(months_between(hire_date, sysdate), 0) > 12;
Slide 50©SoftMoore Consulting
Column Functions
• Column functions can be used to summarize data
• Sample column functions MIN MAX COUNT SUM AVG
• Example select count(*) from orders;
select name from customer c, orders o where c.cust_num = o.cust_num and o.total_invoice in (select max(o.total_invoice) from orders);
Slide 51©SoftMoore Consulting
Counting Distinct Values
• By default, the count function does not eliminate duplicate values. In order to count the number of distinct values, combine the distinct keyword with the count function.
• Example select count(distinct state) as num_states from customer;
Slide 52©SoftMoore Consulting
The GROUP BY Clause
• A GROUP BY clause can be used to group query resultsselect state, count(state) from customer group by state order by state;
• A GROUP BY clause can use more than one column.
• When using the GROUP BY clause, only the column or columns in the GROUP BY expression and the built-in functions can be used in the expressions in the SELECT clause.
Slide 53©SoftMoore Consulting
Using Summary Operatorswith a GROUP BY Clause
• Summary operators (AVG, MAX, SUM, etc.) can be applied to queries about groups of rows. The GROUP BY clause specifies the grouping attributes. select c.cust_num, c.name, avg(o.amount_paid) from customer c, orders o where c.cust_num = o.cust_num group by c.cust_num, c.name;
• As before we could simplify this query using the NATURAL JOIN operator. select c.cust_num, c.name, avg(o.amount_paid) from customer c natural join orders o group by c.cust_num, c.name;
Slide 54©SoftMoore Consulting
The HAVING Clause
• When a query result contains data that is grouped and summarized, a HAVING clause can eliminate some of the groups.
• Example query without a HAVING clause (shows order count for all customers)
select name, phone_num, count(order_num) from customer, orders where customer.cust_num=orders.cust_num group by name, phone_num order by name;
Slide 55©SoftMoore Consulting
The HAVING Clause(continued)
• Example query with a HAVING clause (shows order count for all customers with at least 3 orders)
select name, phone_num, count(order_num) from customer, orders where customer.cust_num=orders.cust_num group by name, phone_numhaving count(order_num) >= 3 order by customer.name;
Slide 56©SoftMoore Consulting