Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction
SQL: Intro
“S.Q.L.” or “sequel”
Supported by all major commercial database systems
Standardized – many new features over time
Interactive via GUI or prompt, or embedded in programs
Declarative, based on relational algebra
SQL: Intro
Data Definition Language (DDL)
CREATE, DROP
Other Commands
indexes, constraints, views, triggers, transactions, authorization, …
SQL
Basic SELECT
Statement
SQL: Basic SELECT
Select A1,A2,…,An
From R1,R2, …,Rm
Where condition
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
Student names and majors for which they've applied
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
Student names and majors for which they've applied
The DISTINCT keyword can be used to return only distinct (different) values.
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
Names and GPAs of students with sizeHS < 1000 applying to CS at
Comsats, and the application decision
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
All large universities (enr>20000) with CS applicants
ERROR:
column
select uName reference
from University, Apply "uname" is
ambiguous
where University.uName = Apply.uName
and enrollment > 20000 and major = 'CS';
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
All large universities (enr>20000) with CS applicants
select University.uName
from University, Apply
where University.uName = Apply.uName
and enrollment > 20000 and major = 'CS';
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
Application information
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
Application information first ordered by descending gpa and then by
ascending enrollment
The ORDER BY keyword is used to sort the result by one or more columns
select Student.sID, sName, GPA, Apply.uName, enrollment, major
from Student, University, Apply
where Apply.sID = Student.sID and Apply.uName = University.uName;
order by gpa desc, enrollment;
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
Applicants to bio majors
The LIKE operator is used in a WHERE clause to search for a specified pattern
in a column.
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
If we simply want to get all attributes, then we can just write
select *.
Select *
from Student, university;
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
Add percentage attribute
University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)