AdventureWorks Database: Combine ProductModelID and Name columns from two tables
SQL Query - AdventureWorks: Exercise-137 with Solution
137. From the following tables write a query in SQL to combine the ProductModelID and Name columns. A result set includes columns for productid 3 and 4. Sort the results by name ascending.
Sample table: Production.ProductModel
Sample table: Production.Product
Sample Solution:
SELECT ProductID, Name
FROM Production.Product
WHERE ProductID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM Production.ProductModel
ORDER BY Name;
Sample Output:
productid|name | ---------+--------------------------------+ 1|Adjustable Race | 122|All-Purpose Bike Stand | 879|All-Purpose Bike Stand | 712|AWC Logo Cap | 2|Bearing Ball | 119|Bike Wash | 877|Bike Wash - Dissolver | 316|Blade | 115|Cable Lock | 843|Cable Lock | 98|Chain | 952|Chain | 324|Chain Stays | 322|Chainring | ...
SQL AdventureWorks Editor:
Practice Online
Contribute your code and comments through Disqus.
Previous: Any distinct businessentityid from the first query not found in the second query.
Next: Add vacation and sick time to find total hours away from work.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
SQL: Tips of the Day
How to avoid the "divide by zero" error in SQL?
Select Case when divisor=0 then null Else dividend / divisor End ,,,
OR:
Select dividend / NULLIF(divisor, 0) ...
Ref: https://bit.ly/3dLj7gS
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook