Queries Flashcards

1
Q

AND operator

A

SELECT model
FROM cars
WHERE color = ‘blue’
AND year > 2014;

  • AND: allows multiple conditions to be combined
  • Records must match both conditions that are joined by AND to be included in result set
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

%Wildcard

A

SELECT name
FROM movies
WHERE name LIKE ‘The%’;

  • can be used in a LIKE operator pattern to match zero or more unspecified character(s)
  • example above will match any movie that begins with The followed by zero or more of any characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

AS clause

A

SELECT name AS ‘movie_title’
FROM movies;

  • AS = aliased / allows columns or tables to be specifically renamed in returned result set
  • example above will rename column ‘name’ to ‘movie_title’ upon return of result set
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

OR operator

A

SELECT name
FROM customers
WHERE state = ‘CA’
OR state = ‘NY’;

  • allows multiple conditions to be combined / records matching either condition joined by OR are included in result set
  • example above will return result set with customers from CA or NY
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

_Wildcard

A

SELECT name
FROM movies
WHERE name LIKE ‘_ove’;

  • can be used in a LIKE operator pattern to match any single unspecified character
  • example above will match any movie which begins with a single character, followed by ‘ove’
  • _ = single character / % = zero or more characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ORDER BY clause

A

SELECT *
FROM contacts
ORDER BY birth_date DESC;

  • can be used to sort the result set by a particular column either alphabetically or numerically / can be ordered two ways:
  • DESC: keyword used to sort results in descending order
  • ASC: keyword used to sort results in ascending order (default sort method)
  • example above will sort results by descending birth_date (youngest first, oldest last - 2000, 1999, 1998, etc)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

LIKE operator

A

SELECT name
FROM movies
WHERE name LIKE ‘Star%’;

  • can be used inside of a WHERE clause to match a specified pattern
  • example above will match any movie with ‘Star’ in its title / beginning with Star / % means anything can come after (Start, Stars, Star Wars, etc)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

DISTINCT clause

A

SELECT DISTINCT city
FROM contact_details;

  • used to select unique values of a column
  • example above will select each unique city from contact_details table (so if there are duplicate Chicago entries in the column, result will show Chicago)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

BETWEEN operator

A

SELECT *
FROM movies
WHERE year BETWEEN 1980 AND 1990;

  • used to filter by a range of values / values can be text, number, or date data
  • example above will match any movies made between 1980 and 1990, inclusive (1990 movies will appear in result)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

LIMIT clause

A

SELECT *
FROM movies
LIMIT 5;

  • used to narrow, or limit, result set to the specified number of rows
  • example above will limit result set to 5 rows
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

NULL values

A

SELECT address
FROM records
WHERE address IS NOT NULL;

  • NULL = no value / NOT NULL = some value
  • can be matched, or unmatched, using the IS NULL or IS NOT NULL operators in combination with WHERE clause
  • example above will return addresses that have a value, or NOT NULL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

WHERE clause

A

SELECT title
FROM library
WHERE pub_year = 2017;

  • used to filter rows that match a certain condition
  • example above will return titles with pub_year of 2017 only (= must be equal to 2017)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly