BoostedMonkey
BoostedMonkey

Reputation: 134

SQL condition statement for strings

I am trying to Write a query that produces the title of every action film with a rating of PG-13, R, or NC-17.

Here is my SQL:

SELECT title 
FROM film
INNER JOIN film_category ON film.film_id = film_category.film_id
INNER JOIN category ON film_category.category_id = category.category_id
WHERE category.name = "Action"
WHERE film.rating = "PG-13" OR "R" OR "NC-17";

I believe the problem is in my syntax in my film.rating conditioning. I am new to SQL. I would appreciate if someone could show me in the right direction!

Upvotes: 2

Views: 6112

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269623

You have several errors in your SQL. I would write this as:

SELECT f.title 
FROM film f INNER JOIN
     film_category fc
     ON f.film_id = fc.film_id INNER JOIN
     category c
     ON fc.category_id = c.category_id
WHERE c.name = 'Action' AND f.rating IN ('PG-13', 'R', 'NC-17');

Notes:

  • Table aliases make the query easier to write and to read.
  • Qualify all column names when you have more than one table in the query.
  • WHERE is used at most one time per SELECT.
  • OR is for boolean expressions, not constants. You want IN.
  • Single quotes are the SQL standard for string delimiters.

Upvotes: 2

Sebastian Brosch
Sebastian Brosch

Reputation: 43574

Try the following:

SELECT title 
FROM film f INNER JOIN film_category fc ON f.film_id = fc.film_id
    INNER JOIN category c ON fc.category_id = c.category_id
WHERE c.name = 'Action'
AND f.rating IN ('PG-13', 'R', 'NC-17');

Upvotes: 2

Related Questions