1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB)...

9
1/18/00 CSE 711 data mining 1 What is SQL? uery language for structural databases (esp. RDB) Structured Query Language riginated from Sequel 2 by Chamberlin et al (1976) at IBM’s San Jose Research Lab. NSI (American National Standards Institute) publish an SQL standard in 1986

Transcript of 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB)...

Page 1: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 1

What is SQL?

• Query language for structural databases (esp. RDB) Structured Query Language

• Originated from Sequel 2 by Chamberlin et al (1976) at IBM’s San Jose Research Lab.

• ANSI (American National Standards Institute) published an SQL standard in 1986

Page 2: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 2

Role of SQL?

• Data Definition Language (DDL)

• Interactive data manipulation language (DML)

• Embeded data manipulation language

• View definition

• Integrity

• Transaction Control

Page 3: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 3

Information Retrieval of SQL?

• The select clause is used to list the attributes desired in the result of a query (specifies the columns)• The from clause lists the relations to be scanned in the evaluation of the expression (specifies the tables)• The where clause consists of a predicate involving attributes of the relations that appear in the from clause. (specifies the rows)

select A1, A2, . . ., An

from r1, r2, . . ., rm

where p

Page 4: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 4

Column Manipulation of SQL?

• Choosing Columns SELECT fname, salary FROM employee

• Rearranging the Order of Columns SELECT salary, fname FROM employee

• Insert Literals SELECT fname, ‘Salary: ’, salary FROM employee

• Changing Column Headings SELECT First_name = fname, salary FROM employee

Page 5: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 5

Manipulating Data of SQL?

• Manipulating Numerical Data (Arithmetic op., Math. Func.) SELECT fname, (salary * 1.1) FROM employee

• Manipulating Character Data (String func.) SELECT SUBSTRING(fname,1,1), salary FROM employee

• Manipulating Datetime Data SELECT fname, birthdate, DATEDIFF(year, birthdate, getdate())

FROM employee

• System Functions SELECT ‘database’ = db_name(), ‘user’ = user_name(), login = suser_name()

Page 6: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 6

Choosing Rows of SQL?

• Based on Comparisons SELECT fname, (salary * 1.1) FROM employee

WHERE state = ‘NY’

• Based on Ranges SELECT fname, (salary * 1.1) FROM employee

WHERE birthdate BETWEEN ‘1/1/1950’ and ‘12/31/1970’

• Based on Lists SELECT fname, salary

FROM employeeWHERE major IN (‘CS’, ‘CSE’, ‘ECE’, ‘IS’)

Page 7: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 7

More on Choosing Rows of SQL?

• Based on Character Strings SELECT fname, lname FROM professor

WHERE lname LIKE ‘%hari%’

• Based on Unknown values SELECT fname

FROM employee WHERE salary IS NULL

• Based on Several Search Arguments SELECT fname, salary

FROM employeeWHERE (major LIKE ‘CS%’, OR state =‘NY’)

AND (salary > 25,000)

• Eliminating Duplicates SELECT DISTINCT city FROM authors

• Sorting SELECT fname, lname, salary

FROM employee ORDER BY salary DESC

Page 8: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 8

• Generate Summary Values avg, count, max, min, sum, etc.

(New “Group by” and “Having” clauses)

SELECT title_id, copies_sold = SUM(qty)FROM sales GROUP BY title_idHAVING SUM(qty) > 30

Aggregate Functions of SQL?

Page 9: 1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.

1/18/00 CSE 711 data mining 9

• Joining Tables (Inner Joins, Cross Joins, Outer Joins, Joins with More than Two Tables, Self Joins)

• Nesting SELECT Statements

• Subqueries

• Select Into

• UNION Operator

etc. about SQL?