CSL 451 Introduction to Database Systems Introduction to SQL (3.1-3.4) Department of Computer Science and Engineering Indian Institute of Technology Ropar Narayanan (CK) Chatapuram Krishnan!
Summary Parts of SQL Language Data-definition language Data-manipulation language SQL Data Definition Basic data types Difference between char and varchar Schema definition Primary key Foreign key Referential integrity constraint not null constraint create, insert, alter, drop and delete 2!
Summary SQL Query Structure SELECT clause all and distinct Attribute specification * FROM clause WHERE clause Logical connectives and, or and not Comparison operators between and not between Order of processing the clauses Natural Join Rename operator as Correlation name String Operations Special characters % and _ like Case sensitivity upper and lower trim Ordering the display of tuples order by, asc, and desc 3!
3.1.a Find the titles of courses in the Comp. Sci. department that have 3 credits 4!
3.1.b Find the IDs of all students who were taught by an instructor named Einstein; make sure there are no duplicates in the result. 5!
3.6 Write a query that finds departments whose names contain the string sci as a substring, regardless of the case. 6!
3.6 Find all students with names starting in m and taking courses offered by Comp. Sci. department 7!
3.7 Consider the SQL query select distinct p.a1 from p, r1, r2 where p.a1=r1.a1 or p.a1=r2.a1 Under what conditions does the preceding query select values of p.a1 that are either in r1 or in r2? 8!
3.8.b Find the names of all customers who live on the same street and the same city as Smith branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) loan(loan_number, branch_name, amount) borrower (customer_name, loan_number) account (account_number, branch_name, balance) depositor (customer_name, account_number) 9!
3.8.c Find the names of all branches with customers who have an account in the bank and who live in Harrison branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) loan(loan_number, branch_name, amount) borrower (customer_name, loan_number) account (account_number, branch_name, balance) depositor (customer_name, account_number) 10!
3.9.a Find the names and cities of residence of all employees who work for First Bank Corporation employee (employee_name, street, city) works (employee_name, company_name, salary) company (company_name, city) manages (employee_name, manager_name) 11!
3.9.b Find the names and cities of residence of all employees who work for First Bank Corporation and earn more than $10,000 employee (employee_name, street, city) works (employee_name, company_name, salary) company (company_name, city) manages (employee_name, manager_name) 12!
3.9.c Find all employees in the database who do not work for First Bank Corporation employee (employee_name, street, city) works (employee_name, company_name, salary) company (company_name, city) manages (employee_name, manager_name) 13!
3.16.b Find all employees in the database who live in the same cities for which they work employee (employee_name, street, city) works (employee_name, company_name, salary) company (company_name, city) manages (employee_name, manager_name) 14!
3.16.c Find all employees in the database who live in the same cities and on the same streets as do their managers employee (employee_name, street, city) works (employee_name, company_name, salary) company (company_name, city) manages (employee_name, manager_name) 15!
3.13 Write the SQL DDL corresponding to the following schema person (driver id, name, address) car (license, model, year) accident (report number, date, location) owns (driver id, license) participated (report number, license, driver id, damage_amount) 16!
3.14 Find all the accident related information involving cars belonging to John Smith person (driver id, name, address) car (license, model, year) accident (report number, date, location) owns (driver id, license) participated (report number, license, driver id, damage_amount) 17!
Find all the car models that were involved in an accident in the year 2009. person (driver id, name, address) car (license, model, year) accident (report number, date, location) owns (driver id, license) participated (report number, license, driver id, damage_amount) 18!
EN 4.1 Why does SQL allow duplicate tuples in a table or in a query result? 19!
EN 4.8 Write appropriate SQL DDL statements for the following LIBRARY database 20!