w3resource

AdventureWorks Database: A distinct businessentityid must be returned by both queries

SQL Query - AdventureWorks: Exercise-135 with Solution

135. From the following tables write a query in SQL to fetch distinct businessentityid that are returned by both the specified query. Sort the result set by ascending order on businessentityid.

Sample table: Person.BusinessEntity


Click to view Full table

Sample table: Person.Person


Click to view Full table

Sample Solution:

SELECT businessentityid   
FROM person.businessentity    
INTERSECT   
SELECT businessentityid   
FROM person.person
WHERE person.persontype = 'IN'  
ORDER BY businessentityid;

Sample Output:

businessentityid|
----------------+
            1699|
            1700|
            1701|
            1702|
            1703|
            1704|
            1705|
            1706|
            1707|
            1708|
            1709|
            1710|
            1711|
...	

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Fetch any distinct values from left query of EXCEPT that aren't present in the query to the right.
Next: Any distinct businessentityid from the first query not found in the second query.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

How to select rows with no matching entry in another table?

SELECT t1.ID
FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL

Ref: https://bit.ly/2QPhaD3

 





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