SQL Part 1 Flashcards
(22 cards)
What keyword is used for alias’s?
AS
Can you use string literals? If so, what would you use?
Yes, single quotes
What happens with the following line: SELECT 0 as id FROM Student
All student ID’s will show up as 0’s
What is the syntax for pattern matching?
LIKE ‘%_%’ where _ is any one character and % can mean any character. % can be placed between, before, and after characters.
List all students, sort them by GPA (descending) then name (ascending). The Student table has columns name and GPA.
SELECT GPA, name FROM Student ORDER BY GPA DESC, name;
Show me the names and phone numbers of passengers whose phone numbers have exactly two 7’s. Table is PASSENGER with columns passengername, phone
SELECT passengername, phone
FROM passenger
WHERE phone LIKE ‘%7%7%’ AND NOT phone LIKE ‘%7%7%7%’
Which semantics are set to default by SQL, Set or Bag?
Bag
What keyword is used to remove duplicates?
DISTINCT
What is used to access qualifying names and specific data structures? What is the format?
The ‘.’ symbol. Format: schema_name.table_name.column_name
How do you explicitly state there is a single quote in your equivalency statement?
Use another single quote, so ‘’
From Beers(name, manf), find all pairs of beers made by the same manufacturer.
SELECT b1.name, b2.name
FROM Beers b1, Beers b2
WHERE b1.manf = b2.manf and b1.name <> b2.name;
How do you say NOT EQUAL in SQL?
<> or !=
What is the syntax of a join statement?
Table1 JOIN Table2 ON Table1.Attribute = Table2.Attribute
Using Likes(drinker, beer), Sells(bar, beer, price), and Frequents(drinker, bar), find the drinkers and beers such that:
1. The drinker likes the beer
2. The drinker frequents at least one bar that sells the beer
(SELECT * FROM Likes) INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar);
What subquery keywords are used for combination/omission of their sets with each other?
UNION, INTERSECT, DIFFERENCE
What are all aggregate SQL functions?
MIN(), MAX(), COUNT(), AVG(), SUM()
How do NULL’s work in operations?
The result will become NULL when operating on it.
Whats the difference between INNER JOIN and OUTER JOIN?
INNER JOIN (JOIN) only includes the rows that match between the two tables. OUTER JOIN includes all rows in both tables, even if there was no match between the two.
Using Sells(bar, beer, price), find the bars that serve Miller for the same price Joe’s Bar charges for Bud.
SELECT bar
FROM Sells
WHERE beer = ‘Miller’ AND
price = (SELECT price
FROM Sells
WHERE bar = ‘Joe’’s Bar’ AND beer = ‘Bud’)
What symbol is used to query all rows?
*
Find beers liked by at least one person who frequents Joe’s Bar. Use the following tables: Likes(drinker, beer) Frequents(drinker, bar)
SELECT beer
FROM Likes, (SELECT drinker FROM Frequents WHERE bar = ‘Joe’’s Bar’)JD
WHERE Likes.drinker = JD.drinker;
Find the department names that have no employees. departments(id, name) employees(id, name, department_id)
SELECT name FROM departments WHERE id NOT IN (SELECT department_id FROM employees)