DB Lecture 2 SQL
DB Lecture 2 SQL
Query Language
Overview
Introduction
DDL Commands
DML Commands
SQL Statements, Operators, Clauses
Aggregate Functions
Structured Query Language
Characteristics of SQL
3. View Definition:
GRANT, REVOKE
Structured Query Language
COMMIT, ROLLBACK
SQL Commands
Operators in SQL:
The following are the commonly used operators in SQL
Arithmetic Operators +, -, *, /
Relational Operators =, <, >, <=, >=, <>
Logical Operators OR, AND, NOT
Arithmetic operators are used to perform simple arithmetic
operations.
Relational Operators are used when two values are to be
compared and
Logical operators are used to connect search conditions in
the WHERE Clause in SQL.
Example:
CREATE TABLE FoodCart
(
date varchar(10), FoodCart
food varchar(20), date food profit
profit float
);
FoodCart
ALTER TABLE FoodCart ( date food profit sold
ADD sold int
);
FoodCart
FoodCart
date food sold
date food sold 02/25/08 pizza 350
02/25/08 pizza 350 02/26/08 hotdog 500
02/26/08 hotdog 500 02/26/08 pizza 70
SQL: UPDATE Statement
To update the content of the table:
UPDATE statement syntax:
UPDATE <table name> SET <attr> = <value>
WHERE <selection condition>;
Example: UPDATE FoodCart SET sold = 349
WHERE date = ’02/25/08’ AND food = ‘pizza’;
FoodCart
SQL provides two wild card characters that are used while
comparing the strings with LIKE operator.
ORDER BY Clause
ORDER BY Clause
GROUP BY Clause
GROUP BY Clause
GROUP BY city;
SQL: Join operation
A join can be specified in the FROM clause
which list the two input relations and the
WHERE clause which lists the join condition.
Example:
Emp Dept
ID State ID Division
1000 CA 1001 IT
1001 MA 1002 Sales
1002 TN 1003 Biotech
SQL: Join operation (cont.)
inner join = join
SELECT *
FROM emp join dept (or FROM emp, dept)
on emp.id = dept.id;
HAVING Clause
HAVING Clause
COUNT(attr)
SUM(attr)
MAX(attr)
MIN(attr)
AVG(attr)
Ronald R. Plew, Ryan K. Stephens. Teach Yourself SQL in 24 Hours 3rd Edition.
Sams Publishing, 2003.
SQL http://en.wikipedia.org/wiki/SQL
W3C http://www.w3schools.com/sql/sql_tryit.asp