Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Newbie needs help w/ SQL statement

Author  Topic 

mailboxo71
Starting Member

3 Posts

Posted - 2011-01-20 : 22:43:21
Please help me, I can't find any proper tutorial that would properly help me on this.

I have the following database structure, I need a SQL query to retrieve a list of the 5 most popular states that had orders.


--------------------------------------------
Orders
------
id
date
shipping_amount
order_status
customer_id (Customers.id)


Customers
----------
id
first_name
last_name
city
state



The output should look something like this:
+-----------+------------+
| State | # Orders |
+-----------+------------+
| NY | 55 |
| CA | 40 |
| NJ | 33 |
| FL | 21 |
| MO | 12 |
+-----------+------------+

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-01-20 : 22:45:44
What have you tried so far?

I'll give you two hints: (1) INNER JOIN (2) GROUP BY

show us what you have, and we'll help you straighten it out.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-01-20 : 22:47:03
you can't find any tutorial that explains how to do a join? a google of "query results from two tables" returns about 381,000 results, most of which on page 1 would show you how. Smells like a bit of homework, so here is a hint.

CustomerID (From ORDERS) = ID (from CUSTOMERS)



Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

mailboxo71
Starting Member

3 Posts

Posted - 2011-01-21 : 00:20:48
Sorry, I am stumped.

Anyone who can help me compose this, I really appreciate it.
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-21 : 00:37:48
if that really a home work then you should try at least your self... Make any thing even its wrong and post it here we'll step wise guide you :D

by the way if you know little about select statement. so you can get your work done via above hints !!! :D

Go to Top of Page

mailboxo71
Starting Member

3 Posts

Posted - 2011-01-21 : 10:45:08
if u can supply some really relevant tutorials then pls give link.

Ask this forum is like talkin to a brick
Go to Top of Page

KenW
Constraint Violating Yak Guru

391 Posts

Posted - 2011-01-21 : 12:45:43
quote:
Originally posted by mailboxo71

Ask this forum is like talkin to a brick



I suggest you not be rude when you're asking people to help you. Rudeness makes them want to leave you alone to suffer.
Go to Top of Page
   

- Advertisement -