SQL - Queries Flashcards
_______is the clause we use every time we want to query information from a database.
SELECT
SELECT column1, column2
FROM table_name;
_______renames a column or table.
AS
SELECT name AS ‘Titles’
FROM movies;
_______ return unique values.
DISTINCT
SELECT DISTINCT tools
FROM inventory;
_______is a popular command that lets you filter the results of the query based on conditions that you specify.
WHERE
SELECT *
FROM movies
WHERE imdb_rating > 8;
_______and _______are special operators.
LIKE, BETWEEN
SELECT *
FROM movies
WHERE name LIKE ‘A%’;
SELECT *
FROM movies
WHERE year BETWEEN 1990 AND 1999;
_______ and _______ combines multiple conditions.
AND, OR
SELECT *
FROM movies
WHERE year BETWEEN 1990 AND 1999
AND genre = ‘romance’;
SELECT *
FROM movies
WHERE year > 2014
OR genre = ‘action’;
_______ sorts the result.
ORDER BY
SELECT *
FROM movies
ORDER BY name;
_______ specifies the maximum number of rows that the query will return.
LIMIT
SELECT *
FROM movies
LIMIT 10;
_______ creates different outputs
CASE
SELECT name,
CASE
WHEN imdb_rating > 8 THEN ‘Fantastic’
WHEN imdb_rating > 6 THEN ‘Poorly Received’
ELSE ‘Avoid at All Costs’
END
FROM movies;
Find the error in this code:
SELECT name,
CASE
WHEN imdb_rating > 8 THEN ‘Oscar’
WHEN imdb_rating > 7 THEN ‘Good’
WHEN imdb_rating > 6 THEN ‘Fair’
FROM movies;
Missing END statement.
IS NULL condition returns true if the field has no value. T/F?
T
How would you query all the unique genres from the ‘books’ table?
SELECT DISTINCT genres
FROM books;
What is LIKE?
A special operator that can be used with the WHERE clause to search for a pattern.
What does the wildcard character % in the following SQL statement do?
SELECT *
FROM sports
WHERE name LIKE ‘%ball’;
It matches all sports that end with ‘ball’.
What is the correct syntax to query both the name and date columns from the database?
SELECT __________
FROM album;
name, date
What code would you add to this query to place the colors in reverse alphabetical order (Z to A) by name?
SELECT *
FROM colors
_________________;
ORDER BY name DESC
What is ORDER BY?
A clause that sorts the result set alphabetically or numerically.
What is LIMIT?
A clause that lets you specify the maximum number of rows the result set will have.
Which of the following is NOT a comparison operator in SQL?
!=
> =
~
<
~
Quiz: Queries
Which operator would you use to query values that meet all conditions in a WHERE clause?
AND
What is the correct query to select only the cities with temperatures less than 35?
SELECT *
FROM cities;
SELECT *
FROM cities
WHERE temperature != 35;
SELECT *
FROM cities
WHERE temperature = 35;
SELECT *
FROM cities
WHERE temperature < 35;
SELECT *
FROM cities
WHERE temperature < 35;