SlideShare a Scribd company logo
1 of 18
Introduction to SQL 
(Standard Query Language)
Domain Types in SQL 
 char(n). Fixed length character string, with user-specified length n. 
 varchar(n). Variable length character strings, with user-specified 
maximum length n. 
 int. Integer (a finite subset of the integers that is machine-dependent). 
 smallint. Small integer (a machine-dependent subset of the integer 
domain type). 
 numeric(p,d). Fixed point number, with user-specified precision of 
p digits, with n digits to the right of decimal point. 
 real, double precision. Floating point and double-precision floating 
point numbers, with machine-dependent precision. 
 float(n). Floating point number, with user-specified precision of at 
least n digits. 
 More are covered in Chapter 4.
Create Database Construct 
 CREATE DATABASE dbname; 
 Example: 
CREATE DATABASE my_db; 
Create Table Construct 
 CREATE TABLE table_name 
( 
column_name1 data_type(size), 
column_name2 data_type(size), 
.... 
); 
 Example: 
create table instructor ( 
ID char(5), 
name varchar(20) not null, 
dept_name varchar(20), 
salary numeric(8,2))
Integrity Constraints in Create Table 
 not null 
 primary key (A1, ..., An ) 
 foreign key (Am, ..., An ) references r 
Example: Declare ID as the primary key for instructor 
. 
CREATE TABLE Persons 
( 
ID int NOT NULL, 
LastName varchar(255) NOT NULL, 
FirstName varchar(255), 
PRIMARY KEY (ID) 
); 
primary key declaration on an attribute automatically ensures not null
Integrity Constraints in Create Table 
 CREATE TABLE Orders 
( 
O_Id int NOT NULL, 
OrderDetails varchar(255) NOT NULL, 
P_Id int, 
PRIMARY KEY (O_Id), 
FOREIGN KEY (P_Id) REFERENCES Persons(ID) 
);
Insert into table 
 INSERT INTO table_name 
VALUES (value1,value2,value3,...); 
 Example: 
 insert into instructor values (‘10211’, ’Smith’, ’Biology’, 
66000); 
 insert into instructor values (‘10211’, null, ’Biology’, 66000);
Basic Query Structure 
 A typical SQL query has the form: 
select A1, A2, ..., An 
from r1, r2, ..., rm 
where P 
 Ai represents an attribute (desired column names) 
 Ri represents a relation (table names) 
 P is a predicate (Condition)
The select Clause 
 Example: find the names of all instructors: 
select name 
from instructor 
 NOTE: SQL names are case insensitive (i.e., you may use upper- or 
lower-case letters.) 
 E.g. Name ≡ NAME ≡ name 
 Some people use upper case wherever we use bold font.
The select Clause (Cont.) 
distinct 
 Find the names of all departments with instructor, and remove 
duplicates 
select distinct dept_name 
from instructor 
all 
 The keyword all specifies that duplicates not be removed. 
select all dept_name 
from instructor
The select Clause (Cont.) 
 An asterisk in the select clause denotes “all attributes” 
select * 
from instructor 
 The select clause can contain arithmetic expressions involving 
the operation, +, –, , and /, and operating on constants or 
attributes of tuples. 
 The query: 
select ID, name, salary/12 
from instructor 
would return a relation that is the same as the instructor relation, 
except that the value of the attribute salary is divided by 12.
The where Clause 
 To find all instructors in Comp. Sci. dept with salary > 80000 
select name 
from instructor 
where dept_name = ‘Comp. Sci.' and salary > 80000 
 Comparison results can be combined using the logical 
connectives and, or, and not. 
 Comparisons can be applied to results of arithmetic expressions 
(<=, <, >, >=, !=).
Where Clause Predicates 
between 
 Example: Find the names of all instructors with salary between 
$90,000 and $100,000 (that is,  $90,000 and  $100,000) 
 select name 
from instructor 
where salary between 90000 and 100000 
 select name, course_id 
from instructor, teaches 
where (instructor.ID, dept_name) = (teaches.ID, ’Biology’);
The from Clause 
 The from clause lists the relations involved in the query 
select  
from instructor, teaches 
 generates every possible instructor – teaches pair, with all 
attributes from both relations
Drop and Alter Table Constructs 
 drop table customers 
 Deletes the table and its contents 
>>DROP TABLE table_name 
>>DROP DATABASE database_name 
 delete from customers 
 Deletes all contents of table, but retains table 
 DELETE FROM Customers 
WHERE CustomerName='Alfreds Futterkiste' AND 
ContactName='Maria Anders';
Drop and Alter Table Constructs 
 alter table 
 alter table- add 
 ALTER TABLE table_name 
ADD column_name datatype 
 ALTER TABLE Customers 
ADD Customer_Joindate Date 
 alter table- drop 
 ALTER TABLE table_name 
DROP COLUMN column_name 
 ALTER TABLE Customers 
DROP Customer_Joindate Date
The Rename (As) Operation 
 The SQL allows renaming relations and attributes using the as clause: 
old-name as new-name 
 E.g. 
 select ID, name, salary/12 as monthly_salary 
from instructor 
 Find the names of all instructors who have a higher salary than 
some instructor in ‘Comp. Sci’. 
 select distinct T. name 
from instructor as T, instructor as S 
where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’
Like- String Operations 
 percent (%). The % character matches any substring. 
 underscore (_). The _ character matches any character. 
 Find the names of all instructors whose name includes the substring 
“dar”. 
select name 
from instructor 
where name like '%dar%' 
 Match the string “100 %” 
select name 
from instructor 
where attendance like ‘100 %' escape ''
Order by - Ordering the Display of 
Tuples 
 List in alphabetic order the names of all instructors 
select distinct name 
from instructor 
order by name 
 We may specify desc for descending order or asc for 
ascending order, for each attribute; ascending order is the 
default. 
 Example: order by name desc 
 Can sort on multiple attributes 
 Example: order by dept_name, name

More Related Content

What's hot

What's hot (20)

Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL2.pptx
SQL2.pptxSQL2.pptx
SQL2.pptx
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql commands
Sql commandsSql commands
Sql commands
 
Structured Query Language
Structured Query LanguageStructured Query Language
Structured Query Language
 
MySQL
MySQLMySQL
MySQL
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
SQL
SQLSQL
SQL
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?
 
Structured Query Language (SQL)
Structured Query Language (SQL)Structured Query Language (SQL)
Structured Query Language (SQL)
 

Similar to SQL : introduction

Similar to SQL : introduction (20)

SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Chapter08
Chapter08Chapter08
Chapter08
 
Module03
Module03Module03
Module03
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6
 
PPT
PPTPPT
PPT
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Ch 3.pdf
Ch 3.pdfCh 3.pdf
Ch 3.pdf
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
75864 sql
75864 sql75864 sql
75864 sql
 
mysql.ppt
mysql.pptmysql.ppt
mysql.ppt
 
Database queries
Database queriesDatabase queries
Database queries
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 

More from Shakila Mahjabin (15)

Computer processing
Computer processingComputer processing
Computer processing
 
Arrays in CPP
Arrays in CPPArrays in CPP
Arrays in CPP
 
CSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL QuestionCSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL Question
 
Normalization
NormalizationNormalization
Normalization
 
Solution of Erds
Solution of ErdsSolution of Erds
Solution of Erds
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
 
Ch1- Introduction to dbms
Ch1- Introduction to dbmsCh1- Introduction to dbms
Ch1- Introduction to dbms
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Algo analysis
Algo analysisAlgo analysis
Algo analysis
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Codes on structures
Codes on structuresCodes on structures
Codes on structures
 
Arrays
ArraysArrays
Arrays
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
 
String operation
String operationString operation
String operation
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 

Recently uploaded

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Recently uploaded (20)

Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 

SQL : introduction

  • 1. Introduction to SQL (Standard Query Language)
  • 2. Domain Types in SQL  char(n). Fixed length character string, with user-specified length n.  varchar(n). Variable length character strings, with user-specified maximum length n.  int. Integer (a finite subset of the integers that is machine-dependent).  smallint. Small integer (a machine-dependent subset of the integer domain type).  numeric(p,d). Fixed point number, with user-specified precision of p digits, with n digits to the right of decimal point.  real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision.  float(n). Floating point number, with user-specified precision of at least n digits.  More are covered in Chapter 4.
  • 3. Create Database Construct  CREATE DATABASE dbname;  Example: CREATE DATABASE my_db; Create Table Construct  CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), .... );  Example: create table instructor ( ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2))
  • 4. Integrity Constraints in Create Table  not null  primary key (A1, ..., An )  foreign key (Am, ..., An ) references r Example: Declare ID as the primary key for instructor . CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), PRIMARY KEY (ID) ); primary key declaration on an attribute automatically ensures not null
  • 5. Integrity Constraints in Create Table  CREATE TABLE Orders ( O_Id int NOT NULL, OrderDetails varchar(255) NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(ID) );
  • 6. Insert into table  INSERT INTO table_name VALUES (value1,value2,value3,...);  Example:  insert into instructor values (‘10211’, ’Smith’, ’Biology’, 66000);  insert into instructor values (‘10211’, null, ’Biology’, 66000);
  • 7. Basic Query Structure  A typical SQL query has the form: select A1, A2, ..., An from r1, r2, ..., rm where P  Ai represents an attribute (desired column names)  Ri represents a relation (table names)  P is a predicate (Condition)
  • 8. The select Clause  Example: find the names of all instructors: select name from instructor  NOTE: SQL names are case insensitive (i.e., you may use upper- or lower-case letters.)  E.g. Name ≡ NAME ≡ name  Some people use upper case wherever we use bold font.
  • 9. The select Clause (Cont.) distinct  Find the names of all departments with instructor, and remove duplicates select distinct dept_name from instructor all  The keyword all specifies that duplicates not be removed. select all dept_name from instructor
  • 10. The select Clause (Cont.)  An asterisk in the select clause denotes “all attributes” select * from instructor  The select clause can contain arithmetic expressions involving the operation, +, –, , and /, and operating on constants or attributes of tuples.  The query: select ID, name, salary/12 from instructor would return a relation that is the same as the instructor relation, except that the value of the attribute salary is divided by 12.
  • 11. The where Clause  To find all instructors in Comp. Sci. dept with salary > 80000 select name from instructor where dept_name = ‘Comp. Sci.' and salary > 80000  Comparison results can be combined using the logical connectives and, or, and not.  Comparisons can be applied to results of arithmetic expressions (<=, <, >, >=, !=).
  • 12. Where Clause Predicates between  Example: Find the names of all instructors with salary between $90,000 and $100,000 (that is,  $90,000 and  $100,000)  select name from instructor where salary between 90000 and 100000  select name, course_id from instructor, teaches where (instructor.ID, dept_name) = (teaches.ID, ’Biology’);
  • 13. The from Clause  The from clause lists the relations involved in the query select  from instructor, teaches  generates every possible instructor – teaches pair, with all attributes from both relations
  • 14. Drop and Alter Table Constructs  drop table customers  Deletes the table and its contents >>DROP TABLE table_name >>DROP DATABASE database_name  delete from customers  Deletes all contents of table, but retains table  DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';
  • 15. Drop and Alter Table Constructs  alter table  alter table- add  ALTER TABLE table_name ADD column_name datatype  ALTER TABLE Customers ADD Customer_Joindate Date  alter table- drop  ALTER TABLE table_name DROP COLUMN column_name  ALTER TABLE Customers DROP Customer_Joindate Date
  • 16. The Rename (As) Operation  The SQL allows renaming relations and attributes using the as clause: old-name as new-name  E.g.  select ID, name, salary/12 as monthly_salary from instructor  Find the names of all instructors who have a higher salary than some instructor in ‘Comp. Sci’.  select distinct T. name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’
  • 17. Like- String Operations  percent (%). The % character matches any substring.  underscore (_). The _ character matches any character.  Find the names of all instructors whose name includes the substring “dar”. select name from instructor where name like '%dar%'  Match the string “100 %” select name from instructor where attendance like ‘100 %' escape ''
  • 18. Order by - Ordering the Display of Tuples  List in alphabetic order the names of all instructors select distinct name from instructor order by name  We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.  Example: order by name desc  Can sort on multiple attributes  Example: order by dept_name, name