Friday, January 27, 2012

Advanced SQL Queries

For Tables Defined in : http://dbms101.blogspot.com/2012/01/tutorial-db.html

1. Get list of customers who have not placed an order yet.
SQL Query:
-------------
select *
from customers left outer join orders on customers.customer_id = orders.customer_id
where orders.order_id is NULL ;

1    Robin Smith    100 Oracle Plaza    Sunnyvale    CA           
2    John Lincoln    200 Oracle Plaza    Sunnyvale    CA           
4    David Crow    400 Oracle Plaza    Sunnyvale    CA           
5    Test User1    500 Oracle Plaza    Santa Clara    CA           
7    Test User3    700 Oracle Plaza    Palo Alto    CA           
8    Test User4    800 Oracle Plaza    Palo Alto    CA          

Analysis:
---------
When you just do
select *
from customers left outer join orders on customers.customer_id = orders.customer_id;
- We get all customer rows.
By adding condition where orders.order_id is NULL, we get the solution.

REFERENCES : http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=27

No comments:

Post a Comment