w3resource

AdventureWorks Database: List all the products that are red or blue

SQL Query - AdventureWorks: Exercise-37 with Solution

37. From the following table write a query in SQL to list all the products that are Red or Blue in color. Return name, color and listprice.Sorts this result by the column listprice.

Sample table: Production.Product


Click to view Full table

Sample Solution:

SELECT Name, Color, ListPrice  
FROM Production.Product  
WHERE Color = 'Red'  
UNION ALL  
SELECT Name, Color, ListPrice  
FROM Production.Product  
WHERE Color = 'Blue'  
ORDER BY ListPrice ASC;

Sample Output:

name                       |color|listprice|
---------------------------+-----+---------+
Sport-100 Helmet, Blue     |Blue |    34.99|
Sport-100 Helmet, Red      |Red  |    34.99|
Classic Vest, S            |Blue |     63.5|
Classic Vest, L            |Blue |     63.5|
Classic Vest, M            |Blue |     63.5|
LL Touring Frame - Blue, 54|Blue |   333.42|
LL Touring Frame - Blue, 50|Blue |   333.42|
LL Touring Frame - Blue, 44|Blue |   333.42|
LL Touring Frame - Blue, 62|Blue |   333.42|
LL Touring Frame - Blue, 58|Blue |   333.42|
LL Road Frame - Red, 58    |Red  |   337.22|
LL Road Frame - Red, 60    |Red  |   337.22|
LL Road Frame - Red, 62    |Red  |   337.22|
LL Road Frame - Red, 44    |Red  |   337.22|
LL Road Frame - Red, 48    |Red  |   337.22|
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Fetch rows from the middle of a sorted table.
Next: Retrieve name and associated salesorders.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

T-SQL Cast versus Convert?

CONVERT is SQL Server specific, CAST is ANSI.

CONVERT is more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST.

Ref : https://bit.ly/3VKT5M8