Lecture 5 - 21 - 01 - 25
Lecture 5 - 21 - 01 - 25
Yogesh K. Meena
CSE, IITGN
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 1 / 19
Structured Query Language (SQL)
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 2 / 19
A brief history
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 3 / 19
Components of SQL
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 4 / 19
Components of SQL
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 5 / 19
Domain Types in SQL
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 7 / 19
Create Table Construct
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 8 / 19
Integrity Constraints in Create Table
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 9 / 19
More Relation Definitions for University Database: example
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 10 / 19
More Relation Definitions for University Database: example
Note: sec_id can be dropped from primary key above, to ensure a student
cannot be registered for two sections of the same course in the same semester
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 11 / 19
More Relation Definitions for University Database: example
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 12 / 19
Updates to tables
Populating table
insert into instructor values (‘10211‘, ‘Smith‘, ‘Biology‘, 66000);
The values are specified in the order in which the corresponding attributes
are listed in the relation schema.
Delete tuple
Remove all tuples from the student relation
delete from student;
Delete Table
Deletes tuples as well the schema of the student relation.
drop table student;
Update table
alter table r add A D;
where A is the name of the attribute to be added to relation r and D is the
domain of A.
All exiting tuples in the relation are assigned null as the value for the new
attribute.
alter table r drop A;
where A is the name of an attribute of relation r
Dropping of attributes not supported by many databases.
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 13 / 19
The select Clause
The select clause lists the attributes desired in the result of a query:
E.g., 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
results of ‘select‘ name from instructor?
results of ‘select‘ dept_name from instructor?
SQL allows duplicates in relations as well as in query results. To force the
elimination of duplicates, insert the keyword distinct after select.
– e.g., Find the department names of all instructors, and remove duplicates?
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 14 / 19
The select Clause (Cont.)
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 16 / 19
The where Clause
The where clause specifies conditions that the result must satisfy
E.g., To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = ‘Comp. Sci.‘
Comparison results can be combined using the logical connectives and,
or, and not. E.g., To find all instructors in Comp. Sci. dept with salary >
80000
select name
from instructor
where where dept_name = ‘Comp. Sci.‘ and salary > 80000
Comparisons can be applied to results of arithmetic expressions.
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 17 / 19
The from Clause
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 18 / 19
Acknowledgments/Contributions
Yogesh K. Meena (IIT Gandhinagar) Structured Query Language (SQL) January 21, 2025 19 / 19