5.SQL - Queries, Programming, Triggers With
Complete Solutions
What are the components of a simple SQL query? - ANSWER SELECT [DISTINCT]
target-list
FROM relation-list
WHERE qualification
relation-list - ANSWER A list of relation names (possibly with a range-variable after each
name).
target-list - ANSWER A list of attributes of relations in relation-list
qualification - ANSWER Comparisons (Attr op const or Attr1 op Attr2, where op is one of
<, >, =, <=, >=, != ) combined using AND, OR and NOT.
DISTINCT - ANSWER is an optional keyword indicating that the answer should not
contain duplicates. Default is that duplicates are not eliminated!
Are the input and result tables of an SQL query sets or multisets? - ANSWER If not using
DISTINCT, the result tables are a set, else, multiset.
How can you obtain a set of tuples as the result of a query? - ANSWER Tables are
multisets of tuples.
What are range variables in SQL? - ANSWER Really needed only if the same relation
appears twice in the FROM clause. It is good style, however, to use range variables
always!
, SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND bid=103
How can you give names to output columns in a query that are defined by arithmetic or
string expressions? - ANSWER Each item in a select-list can be of the form expression
AS column_name, where expression is any arithmetic or string expression over column
names (possibly prefixed by range variables) and constants, and column_name
is a new name for this column in the output of the query.
SELECT S.age, age1=S.age-5, 2*S.age AS age2
FROM Sailors S
WHERE S.sname LIKE 'B_%B'
What is SQL's support for string pattern matching? - ANSWER SQL supports ___________
via the LIKE operator, as well as using the wild-card symbols % (representing zero or
more arbitrary characters) and ~ (representing exactly one, arbitrary, character).
Hence, '_AB%' represents a ___________ any string that has at least three characters
where its second and third characters are, respectively, A and B.
What operations does SQL provide over (multi)sets of tuples, and how would you use
these in writing queries? - ANSWER UNION, INTERSECT, and EXCEPT
What are nested queries? - ANSWER A very powerful feature of SQL: a WHERE clause
can itself contain an SQL query! (Actually, so can FROM and HAVING clauses.)
Find names of sailors who have reserved boat #103:
SELECT S.sname
FROM Sailors S
WHERE S.sid IN (SELECT R.sid
FROM Reserves R
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller Easton. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $13.99. You're not tied to anything after your purchase.