Databases Flashcards
How do you remove duplicates from a field in SQL?
SELECT DISTINCT(fieldname)
How to only return a subset of results from select?
LIMIT
How would you return the 10th-15th results of a select statement?
LIMIT 9,5
How do you sort by more than one column?
ORDER BY field1, field2, field3
How to do >= x or <= y?
BETWEEN x and y
How should you use quotes?
single quotes for text column names like ‘favorite food’
What do you need to escape in column names?
Single quotes need an extra quote before them
What are the logical operators?
AND, OR, IN, NOT
How to combine two fields into one?
CONCAT(thing,thing,thing)
How to rename a result?
SELECT thing AS other_thing
What arithmetic operations does it support?
+ * /
What are the LIKE metacharacers?
_ matches a single character, % matches 0 or more
How can you join without using JOIN?
Select T1.thing, T2.thing from T1,T2 WHERE T1.key = T2.key;
What are the types of JOIN? Explain each. Which is used when just JOIN is specified?
Inner join (default)- intersection of two tables, use ON keyword
Left (Outer) Join - Returns all from left table, even if there are no matches in right
Right (Outer) Join - Opposite of left join
How to combine rows from two tables in to one? How to do it by removing duplicates? What if the columns don’t’ match exactly?
select rows from T1 U selet rows from T2. U is UNION ALL, and UNION removes duplicates. Use a null for one set if it doesn’t have the field from another set.