d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Database Query Question > Please Help!
Add Reply New Topic New Poll
Member
Posts: 1,754
Joined: Feb 27 2009
Gold: 485.00
Mar 24 2015 02:55pm
I'm not sure how to construct this sql query in Java:

There is a table of restaurants and a table of addresses. Restaurants to addresses is one to many. How will I find the number of restaurants with the name "McDonalds" in EACH STATE. I need the answer in two columns. State and number of restaurants named "McDonalds" in each of those states.
Member
Posts: 1,754
Joined: Feb 27 2009
Gold: 485.00
Mar 24 2015 03:48pm
I am brand new to SQL querying. Is this At all close to what I should be doing?
Code

SELECT
State
count(state)
FROM
Address
WHERE
Restaurants.name = "Mcdonalds"


This post was edited by SAJ on Mar 24 2015 03:48pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 24 2015 04:21pm
You're gonna need a group by if you use count like that. you'll also need to use both tables.

This post was edited by carteblanche on Mar 24 2015 04:22pm
Member
Posts: 1,754
Joined: Feb 27 2009
Gold: 485.00
Mar 24 2015 05:14pm
So..after adding the restaurants table..

Code
SELECT
State
count(state)
FROM
Address
Restaurants
WHERE
Restaurants.name = "Mcdonalds"
GROUP BY
state


What is it that I need to group by? I decided that I would group by state...

Now i realize that this would just return a count of how many addresses in each state. How do I make it relative to the restaurant name?

This post was edited by SAJ on Mar 24 2015 05:29pm
Member
Posts: 1,754
Joined: Feb 27 2009
Gold: 485.00
Mar 24 2015 05:50pm
Do i need to change the WHERE conditions?

Code
WHERE
Address.restaurantID = Restaurants.ID


that still gives me a weird answer. =(
Member
Posts: 1,967
Joined: Jul 10 2007
Gold: 1,252.00
Mar 24 2015 06:21pm
Your where clause is what makes it relative to restaurant name. Only records where Restaurant.name = 'Mcdonalds' will be counted.

Here's simple visual example of how grouping works:
Code
Select ID,
SUM(VALUES)
FROM myTable
GROUP BY ID



In your case you want to count occurrences of restaurants named Mcdonalds in each state. The group by clause identifies how you want to aggregate your result set. You are correct in using state as your group by since you want to know the count for each state.

Quote (SAJ @ Mar 24 2015 06:50pm)
Do i need to change the WHERE conditions?

Code
WHERE
Address.restaurantID = Restaurants.ID


that still gives me a weird answer. =(


Yes, you need Address.restaurantID = Restaurants.ID in your where clause too. Without it you what's going to happen is the database engine is going to join every record in Address to every record in Restaurant where Restaurant.Name = 'Mcdonalds' since it was told to select from both tables without any instruction on how to link the two together.

ex:



By adding Address.restaurantID = Restaurants.ID to your where clause, records are only joined together when the RestaurantID in Address is equivalent to the ID in Restaurant.

The only other problem I see from your other query is that you didnt't have a comma between your column names and table names. I don't know what the database engine you're using, but usually when you do this what happens is that the first value is selected and the second value is treated as a label. So in your case, I would imagine the data set you were returning was showing states with a heading of count(state).

In theory this query should give you what you wanted.

Code
SELECT
State,
count(state)
FROM
Address,
Restaurants
WHERE
Address.restaurantID = Restaurants.ID
AND Restaurants.name = "Mcdonalds"
GROUP BY
state


This post was edited by choombawoomba on Mar 24 2015 06:23pm
Member
Posts: 1,754
Joined: Feb 27 2009
Gold: 485.00
Mar 24 2015 06:46pm
thank you so much! tips on the way
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll