Comparison Operators

Besides =, SQL has five more comparison operators:

OperatorMeaningExample
=equalsprice = 30
<> or !=doesn't equalprice <> 30
<is less thanprice < 30
>is greater thanprice > 30
<=is less than or equal toprice <= 30
>=is greater than or equal toprice >= 30

<> and != mean the same. <> is the one from the SQL standard, which every database knows; != you may know from other programming languages. This course uses <>.

Watch the boundary. “Cheaper than $15” is price < 15, and the games that cost exactly $15 aren't in it:

SELECT title, price
FROM games
WHERE price < 15
ORDER BY price;
titleprice
Cube Logic5
Word Quest10

“$15 at most” is price <= 15, and the three games for $15 are in it too:

SELECT title, price
FROM games
WHERE price <= 15
ORDER BY price, title;
titleprice
Cube Logic5
Word Quest10
Bakery Story15
Ocean Deep15
Pixel Racer15

Either side of a comparison can also be a calculation. A customer has $50 and wants two copies of the same game, one for a friend. The games whose two copies cost $50 at most, the most expensive first:

SELECT title, price * 2 AS two_copies
FROM games
WHERE price * 2 <= 50
ORDER BY price DESC, title;
titletwo_copies
Mystery Manor50
Shadow Ninja50
Tiny Kingdoms50
……

Typical mistake: the signs the other way round, => instead of >=. WHERE price => 40 ends with an error:

Error on line 1: near ">": syntax error

The equals sign always comes second, in the same order as you say it: “greater than or equal to”.

Instructions

Show the title and the price of the games that cost $40 or more, the most expensive first. Sort the games with the same price by title:

titleprice
Knight's Oath60
Goal Rush50
Ice Hockey Pro45
Dragon Forge40
Robot Rebellion40

Below it, show the title and the age rating of the games that aren't suitable from the age of 3 (their age_rating isn't 3). Sort them by the age rating, from the lowest, and then by title:

titleage_rating
Ocean Deep7
Tiny Kingdoms7
Word Quest7
Castle Siege12
……

And for the accounts: show the order items for which the customer paid more than $50 in total, that is, those whose quantity * price is more than 50. Show the columns order_id and game_id, and the total named total, sorted by order_id:

order_idgame_idtotal
221460
261560
271560
291560

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

2/12