2 Structured Query Language
2 Structured Query Language
LEARNING OBJECTIVES
1. It provides a procedural way to query a database. 113 John 12, SP Road Coin collection
2. Input is one (or) more relations. 114 Mary 16, SP Road Painting
3. Output is one relation. 115 Brat 18, GP Road Stamp collection
4.22 | Unit 4 • Databases
σ Hobby = ‘stamp. Collection’(person) In the output table, John name has appeared once, project
The above given statement displays all tuples (or) records operation eliminated duplicates.
with hobby ‘stamp collection’. 2. pName, address (person)
Output: Output:
Id Name Address Hobby Name Address
Intersection B
Like union, Intersection operator requires its operands to Supplier number Supplier number Status City
be of the same type. Given are two relations a and b of the SN3 SURESH 40 HYDERABAD
same type, then, the intersection of those two relations, ‘a’ SN4 RAMESH 30 CHENNAI
INTERSECT ‘b’, is a relation of the same type, with body
consisting of all tuples t such that t appears in both ‘a’ and UNION (A ∪ B)
‘b’.
Supplier number Supplier name Status City
Intersection operation returns tuples which are common
SN1 MAHESH 40 HYDERABAD
to both tables
SN3 SURESH 40 HYDERABAD
Table 3 Result of intersection operation SN4 RAMESH 30 CHENNAI
Example: Consider the given database scheme and the 1. In co-related nested queries, the inner query depends
statement: on the outer query for its value.
2. Sub-query is executed repeatedly, once for each row
EMPLOYEE that is selected by the outer query.
FNAME INITIAL LNAME ENO DOB ADDRESS SALARY DNO 3. A correlated subquery is a sub query that contains a
reference to a table that also appears in the outer query.
DEPARTMENT
Example: Consider the following correlated nested query:
SELECT *
D NAME DNO MANAGER-NO FROM table1
WHERE col1 ≥ ALL
DEPARTMENT–LOCATIONS
(SELECT col1 FROM table2 WHERE table2. col2 =
table1. col2)
DNO D-LOCATION
1. The subquery contains reference to a column of table1,
even though the sub-queries FROM clause does not
PROJECT mention a table table1
2. SQL has to check outside the sub-query and find Table
PNAME PNO P-LOCATION DNO
1 in the outer query
3. Suppose that Table 1 contains a row where col1 = 3 and
col2 = 4 and Table 2 contains a row where col1 = 5 and
WORKS–ON
col2 = 4
ENO PNO HOURS 4. The expression
Example: SELECT DISTINCT ENO FROM WORKS - 1. In this, there is no specific series of operations.
ON WHERE (PNO, HOURS) IN (SELECT PNO, HOURS 2. Relational algebra defines the sequence of operations.
FROM WORKS - ON WHERE ENO = 929). 3. Relational calculus is closer to how users would for-
mulate queries, in terms of information requirements,
This query will select the employee numbers of all rather than in terms of operations.
employees who work on the same (PROJECT, HOURS)
combination on some project a particular employee whose Relational Calculus
ENO = ‘929’ works on. In this example, the IN operator
compares the subtuple of values in parentheses (PNO,
HOURS) for each tuple in works on with the set of union- Tuple Relational Domain Relational
compatible tuples produced by the nested query. calculus (variables Calculus (variables
range over tuples) range over domain
Correlated nested queries: Nested queries can be evaluated attributes)
by executing the sub query (or) Inner query once and sub-
stituting the resulting value (or) values into the WHERE 4. Relational calculus is based on predicate logic, gives
clause of the outer query. the usual quantifiers to construct complex queries.
4.26 | Unit 4 • Databases
sname age
A non-procedural language, where each query is of the
yuppy 35
form {t| p(t)}. It is a set of all tuples t such that predicate p
rusty 35
is true for t, t is a tuple variable, t [A] denotes the value of
tuple ‘t’ on attribute A.
{T| p (T)} Join Operation in Tuple Relational Calculus
T is a tuple and P (T) denotes a formula in which tuple vari- Examples:
able T appears. 3. Find sailors rated > 7 who have reserved boat = 103.
In the above table, we have studentId that uniquely identifies Foreign key: A foreign key is a column or group of columns
the students in a student table. This would be a candidate in a relational database table that provides connectivity
key. between data in two tables.
In the same table, we have student’s first name and last 1. The majority of tables in a relational database system
name, which are also candidate keys. adhere to the concept of foreign key.
1. If we combine first name and last name then also it 2. In complex databases, data must be added across mul-
becomes a candidate key. tiple tables, thus the link or connectivity has to be
2. Candidate key must (have) maintained among the tables.
•• Unique values 3. The concept of Referential Integrity constraint is
•• No null values derived from Foreign key.
•• Minimum number of fields to ensure uniqueness. Example: Emp
•• Uniquely identify each record in the table.
EId EName Dept – No
3. The candidate keys which are not selected for primary
key are known as secondary keys or alternative keys.
Dept
Primary key: A primary key is a candidate key that is most Dept-No DName
suitable (or) appropriate to become main key of the table.
1. It is a special relational database table column ((or)
In the above specified tables, Dept-No is common to both
combination of columns)
the tables, In Dept table it is called as primary key and in
2. Primary key main features are
Emp table it is called as foreign key.
•• It must contain a unique value for each row of data.
These two tables are connected with the help of
•• It cannot contain null values.
‘Dept-No’ field
Example: We can choose primary key as studentId which 1. For any column acting as a foreign key, a correspond-
is mentioned in the table given in above example. ing value should exist in the link (or) connecting table.
Composite primary key: A key that consists of two or more 2. While inserting data and removing data from the for-
attributes that uniquely identify an entity is called compos- eign key column, a small incorrect insertion or dele-
ite key or composite primary key. tion destroys the relationship between the two tables.
Example: Customer
SQL Commands
Cust-Id Order-Id Sale-details
SELECT statement
C1 O–2 Sold
The most commonly used SQL command is SELECT state-
C1 O–3 Sold
ment. The SQL SELECT statement is used to query or retrieve
C2 O–2 Sold data from a table in the database. A query may retrieve infor-
C2 O–3 Sold mation from specified columns or from all of the columns in
the table. To create a simple SQL SELECT statement, you
Composite primary key is {cust-Id, order-Id} must specify the column(s) names and the table name.
Super key: A super key is a combination of attributes that Syntax: SELECT column-name (s) from table name
can be uniquely used to identify a database record. A table
can have any number of super keys. Example: Persons
Lastname Firstname Address City
1. Candidate key is a special subset of super keys.
Hansen Ola SpRoad,-20 Hyd
Example: Customer
Svendson Tove GPRoad,-18 Secbad
Customer name Customer Id SSN Address DOB Petterson Kari RpRoad,-19 Delhi
Male-Female We may specify ‘desc’ for descending order (or) ‘asc’ for
ascending order. - ‘asc’ is default.
Female name Female job Male name Male job
Komal Clerk Rohit Clerk Example: ORDERBY customer-name desc.
Komal Clerk Raju Sales
Join (⋈)
Ankita Sales Rohit Clerk SQL Join is used to get data from two (or) more tables,
Ankita Sales Raju Sales which appear as single table after joining.
2. Find the name, loan-no and loan amount of all custom- SELECT *
ers, rename the column name loan-no as loan.id? FROM Class INNER JOIN Info
Solution: SELECT customer.name, borrower.loan no AS WHERE Class.SID = Info.SID
loan.id, amount FROM borrower, loan, WHERE borrower. Result:
loan-no = loan.loan-no
SID Name SID City
Both tables being joined have SID column (same name and Example: Consider the tables class 1 and Info1
same data type), the tuples for which value of SID matches Query:
in both the tables, appear in the result. SELECT *
FROM class1 FULL OUER JOIN Info1
Dangling tuple: When NATURAL JOIN is performed on ON(class1.SID = Info1.SID)
two tables, there would be some missing tuples in the result
Result:
of NATURAL JOIN
SID Name City
Those missing tuples are called Dangling tuples. In the
16 Arun Chennai
above example, the number of dangling tuples is 1 that is
17 Kamal NULL
14 Adam 18 NULL Noida
OUTER JOIN: Outer Join is based on both matched and ALTER command: ALTER command is used for altering the
unmatched data. table structure
LEFT OUTER JOIN: Left outer Join returns the tuples 1. It is used to add a new column to existing table.
available in the left side table with the matched data of 2 2. To rename existing column.
tables and null for the right tables column. 3. ALTER is used to drop a column.
Example: Consider the table’s class and Info 4. It is used to change data type of any column or modify
SELECT * its size.
FROM class LEFT OUTER JOIN Info Add new column: By using alter command, we can add a
ON(class.SID = Info. SID) new column to the table.
Result:
Syntax: ALTER table table-name ADD(column-name data
SID Name City type).
11 Ana Banglore
Example: Consider a student table.
12 Bala Delhi
SID S Name Grade
13 Sudha Hyderabad
14 adam NULL
RIGHT OUTER JOIN: RIGHT OUTER JOIN returns the Add a new column called address
tuples available in the Right side table with the matched ALTER table student ADD (address char);
data of 2 tables and NULL for the left table’s column.
Example: Class 1 Example: Add multiple columns, parent-name, course-
Name, date-of-birth to student table.
SID Name
16 Arun ALTER table student ADD (parent-name
17 Kamal varchar(60), course-Name varchar(20),
date-of-birth date);
Info 1
SID City Example: Change the data type of column address to varchar?
16 Chennai
17 Noida ALTER table student modify(address varchar(30))
Chapter 2 • Structured Query Language | 4.33
Exercises
Practice Problems 1 (C) SELECT *
Directions for questions 1 to 20: Select the correct alterna- FROM Persons
tive from the given choices. WHERE last-name=’svendson’
AND (first-name=’tove’ AND first-name=’ola’)
1. Consider the given table called Persons (D) SELECT *
FROM Persons
P-Id Lastname Firstname Address City
WHERE last-name=’svendson’
1 Hansen ola Timoteivn -10 Sandnes OR (first-name=’tove’ AND first-name=’ola’)
2 Svendson Tove Brazil-50 Sandnes 3. Write an SQL statement to add a new row, but only in
3 Petterson Kari Storgt-20 Stavanger specified columns, for the persons table add data into
4 Joseph ole Brazil-20 Sandnes columns ‘P-Id’, ‘Last name’ and the ‘First name’ with
values (5, Teja, Jakob)?
Write a query to select the persons with first name (A) INSERT INTO Persons VALUES(5,‘teja’,‘jakob’)
‘Tove’ and last name ‘Svendson’? (B) INSERT INTO Persons VALUES(5,teja,jakob)
(A) SELECT * (C) INSERT INTO Persons (P-Id, last-name, first-
FROM Persons name) VALUES(5,’teja’,’jakob’)
WHERE first-name=’tove’ (D) INSERT INTO Persons(P-Id, last-name, first-
AND last-name=’svendson’ name) VALUES(5,teja,jakob)
(B) SELECT * 4. Write an SQL statement:
FROM Persons (i) To select the persons living in a city that starts
WHERE first-name=’tove’ with ‘S’ from the ‘Persons’ table?
OR last-name=’svendson’ (A) SELECT *
(C) SELECT first-name FROM Persons
FROM Persons WHERE city LIKE ‘s__’.
WHERE first-name=’tove’ (B) SELECT *
AND last-name=’svendson’ FROM Persons
(D) SELECT last-name WHERE city LIKE ‘s%’.
FROM Persons (C) SELECT *
WHERE first-name=’tove’ FROM Persons
AND last-name=’svendson’ WHERE city LIKE ‘%s’.
(D) SELECT *
2. Write a query to select only the persons with last name
FROM Persons
‘Svendson’ and the first name equal to ‘Tove’ or ‘ola’?
WHERE city LIKE ‘_s%’.
(A) SELECT * (ii) To select the persons living in a city that contains
FROM Persons the pattern ‘tav’ from ‘Persons’ table?
WHERE last-name=’svendson’ (A) SELECT *
AND first-name=’tove’ FROM Persons
(B) SELECT * WHERE city LIKE ‘_tav_’.
FROM Persons (B) SELECT *
WHERE last-name=’svendson’ FROM Persons
AND (first-name=’tove’ OR first-name=’ola’) WHERE city LIKE ‘_tav%’.
4.34 | Unit 4 • Databases
(C) SELECT * (i) Find out the students who have scored more than
FROM Persons 80 marks, and display them in descending order
WHERE city LIKE ‘%tav_’. according to their marks?
(D) SELECT * (A) SELECT student-name,marks
FROM Persons FROM Result
WHERE city LIKE ‘%tav%’. WHERE marks > 80
(iii) To select the persons whose last name starts with ORDERBY marks DESC
‘b’ or ‘s’ or ‘p’ ? (B) SELECT *
(A) SELECT * FROM Result
FROM Persons WHERE marks > 80
WHERE last-name LIKE ‘b-s-p’ ORDERBY marks DESC
(B) SELECT * (C) SELECT student-name,marks
FROM Persons FROM Result
WHERE last-name LIKE ‘b%s%p’ WHERE marks > 80
(C) SELECT * ORDERBY marks
FROM Persons (D) (A) and (B)
WHERE last-name LIKE ‘b%s%p%’ (ii)
From the above table, find out the top-most three
(D) SELECT * students.
FROM Persons (A) SELECT student-name
WHERE last-name LIKE ‘[bsp]%’ FROM Result
5. Consider the given table called ‘Persons’ ORDERBY marks DESC > 3
(B) SELECT student-name
P-Id Last-name First-name Address City FROM Result
1 Hansen ola Timoteivn-10 Sandnes ORDERBY marks DESC = 3
2 Svendson Tove Brazil-50 Sandnes (C) SELECT student-name
3 Petterson Kari Storgt-20 Stavanger FROM Result
ORDERBY marks DESC limit 3
and the ‘Orders’ table (D) None of these
O-Id Order No P-Id 8. From the table ‘Results’, Identify the suitable SQL
11 77895 3 expression?
12 44678 3 (i) Find out the student Who stood 2nd?
13 22456 1 (A) SELECT student-name
14 24562 1
FROM Result
ORDERBY marks DESC limit 2
15 34764 5
(B) SELECT student-name
perform NATURAL JOIN operation on both the tables FROM Result
and what is are the O_Id’s displayed in the result? ORDERBY marks DESC limit 1,1
(A) 11, 12, 13 (B) 11, 13, 14 (C) SELECT student-name
(C) 11, 12, 13, 14 (D) 12, 13, 14 FROM Result
6. Write an SQL to perform FULL JOIN operation on
ORDERBY marks DESC limit 1,2
both ‘Person’ and ‘Orders’ tables and What is the num- (D) SELECT student-name
ber of tuples in the Result? FROM Result
(A) 4 (B) 5 ORDERBY marks DESC limit 2,1
(C) 6 (D) 7 (ii) Find out how many students scored > = 80.
(A) SELECT COUNT(*)
7. Consider the given table ‘Result’.
FROM Result
Student Name Marks WHERE marks > = 80
A 55 (B) SELECT COUNT
B 90 FROM Result
C 40 WHERE marks > = 80
(C) SELECT SUM(*)
D 80
FROM Result
E 85
WHERE marks > = 80
F 95 (D) SELECT SUM
G 82 FROM Result
WHERE marks > = 80
Chapter 2 • Structured Query Language | 4.35
Practice Problems 2 7. Select the persons whose hobby is either painting (or)
12. Display the names of the people who are not working (A) Names of the students who are working in either
for any project. projects ‘MS’ or ‘MD’
(A) pname (employee ⋈(pname (employee + work on) (B) Names of the students who are working in both the
(B) pname (employee – pname (employee ∩ work on) projects ‘MS’ or ‘MD’
(C) pname (employee – pname (employee ⋈ work on) (C) Names of the students who are not working in any of
the projects ‘MS’ or ‘MD’
(D) sname (employee – sname (employee ⋈ work on)
(D) None of the above
13. Consider the following tables: 16. ‘All rows corresponding to students whose sno’s are
A B C D C D between 10 and 20
b c e f e f
(i) Select * form student where SNo are between 5
AND 10
a b i j g h
(ii) Select * from student where SNO IN(5, 10)
b c g h (A) Only (i) (B) Only (ii)
b c a d (C) Both (A) and (B) (D) None
d i g h 17. UPDATE account SET
d j j k DA = basic * .2,
d i e f GROSS = basic * 1.3, Where basic > 2000;
(A) The above query displays DA and gross for all
R÷S those employees whose basic is ≥ 2000
A B (B) The above query displays DA and Gross for all
b c employees whose basic is less than 2000
(C) The above query displays updated values of DA as
d i
well as gross for all those employees whose basic
Which of the following statements is true? is > 2000
(A) R ÷ S = pA, B(R) – pA, B(pA, B(R) × S + R) (D) All the above
(B) R ÷ S = pA, B(R) – pA, B(pA, BR × S – R) 18. Given two union compatible relations R1(A, B) and R2(C,
D), what is the result of the operation
(C) R ÷ S = pA, B(R) – pA, B((pA, B(R) × S) – R)
R1 A = CAB = DR2?
(D) R ÷ S = pA, B(R) – pA, B(pA, B(R) × R – S) (A) R1 ∪ R2 (B) R1 × R2
(C) R1 – R2 (D) R1 ∩ R2
Common data for questions 14 and 15: Consider the fol-
lowing schema of a relational data base 19. Which of the following queries finds the clients of banker
student (sno, name, address) Agassi and the city they live in?
project (pno, Pname) work-on (sno, pno) (A) pclient.cname.Ccity(sclient.cname = customer c name (sBanker.
Part (part no, part name, qtyon hand size) name = Aggassi
(client × customer)
Use (sno, pno, part no, number) (B) pClient.c city (sBanker name = ‘Aggasi’(client × customer)
14. List the names of the students who are participating in (C) pclient.c name.Cucity(sclient.c name = ‘Aggasi’ (sclient.name = Cutomer
every project and have used every part. (client × customer)
(A) sname(student ⋈(((Workon) ÷ spro(project)) ∩ (ssno, part (D) p .c name.Cucity(sBankers name = name (sBanker. = agassi (cli-
no
(use) ÷ spart no (part))) ent × customer)
(B) pname(student ⋈(((Workon) ÷ ppro(project)) ∩ (psno, 20. Consider the following schema pertaining to students data
partno
(use) ÷ spart no (part))) Student (rno, name, add)
(C) pname(student ⋈(((Workon) ÷ ppartno (project)) ∩ (psno, Enroll (rno, Cno, Cname) Where the primary keys are
partno
(use) ÷ spart no (part))) shown Underlined. The no. of tuples in the student and
(D) pname(student ∞ (((Workon) ÷ ppro(project)) ∪ (pssno, Enroll tables are 120 and 8 respectively. What are the
partno
(use) ÷ ppart no (part))) maximum and minimum no. of tuples that can be pre-
sent in (student * Enroll) where ‘*’ denotes natural join.
15. The following query gives pname (employee ⋈(work on ÷
(A) 8, 8 (B) 120, 8
ppro (sPname = ‘MS’ AND ‘MD’(project))) (C) 960, 8 (D) 960, 120
4.42 | Unit 4 • Databases
(C) Courses in which only male students are en- 8. Let R and S be relational schemes such that R =
rolled. {a,b,c} and S = {c}. Now consider the following que-
(D) None of the above ries on the database:
I. π R − S (r ) − π R − S (π R − S (r ) × s − π R − S , S (r ))
5. Consider the relation employee (name, sex, super-
visorName) with name as the key. supervisorName II. {t | t ∈ π R − S (r ) ∧ ∀u ∈ s (∃v ∈ r (u = v[ s ] ∧ t
gives the name of the supervisor of the employee = v[ R − S ]))}
under consideration. What does the following Tuple
Relational Calculus query produce? {t | t ∈ π R − S (r ) ∧ ∀v ∈ r (∃u ∈ s (u = v[ s ] ∧ t
III.
e ⋅ name | employee(e) ∧ = v[ R − S ]))}
(A) Only I and II (B) Only I and III 1 ‘Rahul’ 66 1 ‘AC’ 8201
(C) Only I, II and III (D) Only I, III and IV 2 ‘Sourav’ 67 2 ‘SC’ 8201
4.44 | Unit 4 • Databases
3 ‘Anil’ 69 5 ‘AC’ 8203 Let MX and MY denote the respective maximum values
of X and Y among all records in the table at any point
1 ‘SC’ 8204 in time. Using MX and MY, new records are inserted
3 ‘AC’ 8202 in the table 128 times with X and Y values being MX
+ 1, 2 * MY + 1 respectively. It may be noted that each
time after the insertion, values of MX and MY change.
What pids are returned by the following SQL query for the
What will be the output of the following SQL query
above instance of the tables?
after the steps mentioned above are carried out?
SELECT pid SELECT Y FROM T WHERE X = 7; [2011]
FROM Reservation (A) 127 (B) 255
WHERE class = ‘AC’ AND (C) 129 (D) 257
EXISTS (SELECT * 14. Which of the following statements are true about an
FROM Passenger SQL query?
WHERE age > 65 AND P: An SQL query can contain a HAVING clause
Passenger.pid = Reservation.pid) [2010] even if it does not have a GROUP BY clause
(A) 1, 0 (B) 1, 2 Q: An SQL query can contain a HAVING clause
(C) 1, 3 (D) 1, 5 only if it has a GROUP BY clause
11. Consider a relational table r with sufficient number of R: All attributes used in the GROUP BY clause must
records, having attributes A1, A2, … An and let 1 ≤ p ≤ n. appear in the SELECT clause
Two queries Q1 and Q2 are given below. S: Not all attributes used in the GROUP BY clause
need to appear in the SELECT clause [2012]
Q1: π A1 … An (σ Ap=c (r )) where c is a constant.
(A) P and R (B) P and S
Q2: π A1 … An (σ c1 ≤ Ap ≤ c2 (r )) where c1 and c2 are constants. (C) Q and R (D) Q and S
The database can be configured to do ordered indexing 15. Suppose R1(A, B) and R2(C, D) are two relation
on AP or hashing on Ap. Which of the following state- schemas. Let r1 and r2 be the corresponding relation
ments is TRUE? [2011] instances. B is a foreign key that refers to C in R2.
(A) Ordered indexing will always outperform hash- If data in r1 and r2 satisfy referential integrity con-
ing for both queries straints, which of the following is always true?[2012]
(B) Hashing will always outperform ordered index- (A) ΠB (r1) – ΠC(r2) = ∅
ing for both queries. (B) ΠC(r2) – ΠB (r1) = ∅
(C) Hashing will outperform ordered indexing on (C) ΠB (r1) = ΠC (r2)
Q1, but not on Q2.
(D) Hashing will outperform ordered indexing on (D) ΠB (r1) – ΠC (r2) ≠ ∅
Q2, but not on Q1.
Common data for questions 16 and 17: Consider the fol-
12. Database table by name Loan_Records is given below.
lowing relations A, B and C:
Borrower Bank manager Loan amount (A)
Ramesh Sunderajan 10000.00 Id Name Age
16. How many tuples does the result of the following 20. Given the following schema:
SQL query contain? Employees (emp–id, first-name, last– name, hire–
SELECT A.Id date, dept–id, salary)
FROM A Departments (dept–id, dept–name, manager–id,
WHERE A. Age > ALL (SELECT B. Age location–id)
FROM B you want to display the last names and hire dates of
WHERE B. Name = ‘Arun’) [2012] all latest hires in their respective departments in the
(A) 4 (B) 3 location ID 1700. You issue the following query:
(C) 0 (D) 1 SQL > SELECT last–name, hire–date
17. How many tuples does the result of the following FROM employees
relational algebra expression contain? Assume that WHERE (dept–id, hire–date) IN
the schema of A ∪ B is the same as that of A. (SELECT dept–id, MAX (hire–date)
(A⋃B) ⋈A.Id > 40 V C.Id <15C [2012] FROM employees JOIN departments USING
(A) 7 (B) 4 (dept–id)
(C) 5 (D) 9 WHERE location–id = 1700
18. Consider the following relational schema. Students GROUP BY dept–id);
(rollno: integer, sname: string) Courses (courseno: What is the outcome? [2014]
integer, cname: string) Registration(rollno:integer,co (A) It executes but does not give the correct result.
urseno: integer, percent: real) (B) It executes and gives the correct result.
Which of the following queries are equivalent to (C) It generates an error because of pair wise compari-
this query in English? son.
‘Find the distinct names of all students who score (D) It generates an error because the GROUP BY
more than 90% in the course numbered 107’ clause cannot be used with table joins in a sub-
(I) SELECT DISTINCT S.sname FROM Students query.
as S, Registration as R WHERE R.rollno=S.rollno 21. Given an instance of the STUDENTS relation as
AND R.courseno=107 AND R.percent>90 shown below:
(II) psname(σcourseno=107^percent>90 Registration⑅Students) Student Student
Student ID Name Student Email Age CPI
(III) {T |∃ S ∈ Students, ∃R∈ Registration (S.rollno=R.
rollno ∧ R.courseno=107 ∧ R.percent>90∧T. 2345 Shankar shaker @ math X 9.4
sname=S.sname)} 1287 Swati swati @ ee 19 9.5
(IV) {<SN> |∃SR∃RP (<SR, SN> ∈ Students ∧ <SR, 107, 7853 Shankar shankar @ cse 19 9.4
(A) I, II, III and IV (B) I, II and III only 8765 Ganesh ganesh@ civil 19 8.7
(C) I, II and IV only (D) II, III and IV only For (StudentName, StudentAge) to be a key for this
instance, the value X should NOT be equal to _____.
19. Given the following statements: [2014]
S1: A foreign key declaration can always be replaced
22. Consider a join (relation algebra) between relations
by an equivalent check assertion in SQL.
(r(R)) and (s(S)) using the nested loop method. There
S2: Given the table R (a, b, c) where a and b together
are three buffers each of size equal to disk block size,
form the primary key, the following is a valid ta-
out of which one buffer is reserved for intermediate
ble definition.
results. Assuming size r(R) < size s(S), the join will
CREATE TABLE S (
have fewer number of disk block accesses if [2014]
a INTEGER
(A) Relation r(R) is in the outer loop
d INTEGER,
(B) Relation s(S) is in the outer loop
e INTEGER,
(C) Join selection factor between r(R) and s(S) is
PRIMARY KEY (d),
more than 0.5
FOREIGN KEY (a) references R)
(D) Join selection factor between r(R) and s(S) is less
Which one of the following statements is CORRECT? than 0.5
[2014]
23. SQL allows duplicate tuples in relations, and corre-
(A) S1 is TRUE and S2 is FALSE
spondingly defines the multiplicity of tuples in the
(B) Both S1 and S2 are TRUE result of joins. Which one of the following queries
(C) S1 is FALSE and S2 is TRUE always gives the same answer as the nested query
(D) Both S1 and S2 are FALSE shown below:
4.46 | Unit 4 • Databases
Select * from R where a in (select S. a from S)[2014] (C) Names of all the employees with none of their cus-
(A) Select R.* from R, S where R. a = S. a tomers having a ‘GOOD’ rating.
(B) Select distinct R * from R, S where R . a = S . a (D) Names of all the employees with all their customers
(C) Select R.* from R, (select distinct a from S) as S1 having a ‘GOOD’ rating.
where R.a = S1.a 2 7. SELECT operation in SQL is equivalent to [2015]
(D) Select R.* from R, S where R.a = S.a and is unique (A) The selection operation in relational algebra
R (B) The selection operation in relational algebra,
except that SELECT in SQL retains duplicates.
24. What is the optimized version of the relation algebra
(C) The projection operation in relational algebra.
expression π A1 (π A2 (σ F1 (σ F2 ( r ) ))), where A1, A2 are sets
(D) The projection operation in relational algebra,
of attributes in r with A1 ⊂ A2 and F1, F2 are Boolean except that SELECT in SQL retains duplicates.
expressions based on the attributes in r? [2014]
28. Consider the following relations:
π A1 (σ ( F ∧ F )( r ) )
(A)
1 2
Student
π A1 (σ ( F ∨ F )( r ) )
(B) Roll No Student Name
1 2
π A2 (σ ( F ∧ F )( r ) )
(C) 1 Raj
1 2
2 Rohit
π A2 (σ ( F ∨ F )( r ) )
(D) 3 Raj
1 2
EMP
EmpId EmpName DeptName
1 XYA AA SELECTIVE AVG ( EC.Num )
2 XYB AA FROM EC
3 XYC AA
WHERE ( DeptName, Num ) IN
4 XYD AA
(SELECT DeptName, COUNT ( EmpId ) AS
5 XYE AB
6 XYF AB EC ( DeptName, Num )
7 XYG AB FROM EMP
8 XYH AC GROUP BY DeptName)
9 XYI AC
10 XYJ AC
11 XYK AD
12 XYL AD
13 XYM AE
The following query is made on the database. 36. Consider the following two tables and four queries in
SQL.
T 1 ← pCouraseName (sStudentName =' SA ' (CR))
Book (isbn, bname), Stock (isbn, copies)
T 2 ← CR ÷ T 1 Query 1: SELECT B.isbn, S.copies
The number of rows in T2 is . [2017] FROM Book B INNER JOIN Stock S
35. Consider the following database table named ON B.isbn = S.isbn;
top_scorer. Query 2: SELECT B.isbn, S.copies
FROM Book B LEFT OUTER
top_scorer
JOIN Stock S
player country goals
ON B.isbn = S.isbn;
Klose Germany 16
Query 3: SELECT B.isbn, S.copies
Ronaldo Brazil 15 FROM Book B RIGHT OUTER
G Miiller Germany 14 JOIN Stock S
Fontaine France 13 ON B.isbn = S.isbn;
Pelé Brazil 12 Query 4: SELECT B.isbn, S.copies
Klinsmann Germany 11 FROM Book B FULL OUTER
Kocsis Hungary 11 JOIN Stock S
ON B.isbn = S.isbn;
Batistuta Argentina 10
Cubillas Peru 10 Which one of the queries above is certain to have an
Lato Poland 10
output that is a superset of the outputs of the other
three queries? [2018]
Lineker England 10
(A) Query 1 (B) Query 2
T Muller Germany 10
(C) Query 3 (D) Query 4
Rahn Germany 10
37. Consider the relations r(A, B) and s(B, C), where s ⋅ B
Consider the following SQL query: is a primary key and r ⋅ B is a foreign key referencing
SELECT ta.player FROM top_scorer AS ta s ⋅ B. Consider the query
WHERE ta.goals >ALL (SELECT tb.goals Q: r (σB<5 (S))
FROM top_scorer AS tb Let LOJ denote the natural left outer-join operation.
WHERE tb.country = ‘Spain’) Assume that r and s contain no null values.
AND ta.goals >ANY (SELECT tc.goals
Which one of the following queries is NOT equiva-
FROM top_scorer AS tc
lent to Q? [2018]
WHERE tc. country = ‘Germany’)
(A) σB<5(r s) (B) σB<5(r LOJ s)
The number of tuples returned by the above SQL (C) r LOJ (σB<5(s)) (D) σB<5(r) LOJ s
query is _________. [2017]
Answer Keys
Exercises
Practice Problems 1
1. A 2. B 3. C 4. (i) B (ii) D (iii) D 5. C 6. C 7. (i) A (ii) C
8. (i) B (ii) A 9. A 10. A 11. (i) A (ii) B 12. (i) B (ii) B (iii)
A
13. (i)
A (ii) A 14. (i)
A (iii) A (ii) A (iii) A 15. (i) A (ii) A 16. C
A (iii)
17. A 18. D 19. A 20. D
Practice Problems 2
1. B 2. D 3. D 4. A 5. C 6. A 7. A 8. A 9. A 10. A
11. C 12. C 13. C 14. C 15. C 16. B 17. C 18. D 19. B 20. A