Sei sulla pagina 1di 16

SQL

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

Data Manipulation Language (DML)


SELECT, INSERT, DELETE, UPDATE

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

∏A1,A2,…,An ( σcondition (R1x R2x … x Rm))

Demo: simple university admissions database


University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)
SQL: Basic SELECT
IDs, names, and GPAs of students with GPA > 3.6

select sID, sName, GPA


from Student
where GPA > 3.6;

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

select sName, major


from Student, Apply
where Student.sID = Apply.sID;

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.

select distinct sName, major


from Student, Apply
where Student.sID = Apply.sID;

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

select sname, GPA, decision


from Student, Apply
where Student.sID = Apply.sID
and sizeHS < 1000 and major = 'CS' and uname = 'Comsats';

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

select Student.sID, sName, GPA, Apply.uName, enrollment, major


from Student, University, Apply
where Apply.sID = Student.sID and Apply.uName = University.uName;

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.

select sID, major


from Apply
where major like '%bio%';

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

as keyword allows us to change the labeling of the schema in a


query result.

select sID, sName, GPA, sizeHS, (GPA/4)*(100.0) as Percentage


from Student;

University(uName,city,enrollment)
Student(sID,sName,GPA,sizeHS)
Apply(sID,uName,major,decision)

Potrebbero piacerti anche