I have a table of tripadvisor data. There are columns (restaurant, rank, score, user_name,review_stars,review_date,user_reviews....) and other columns that are not useful for my question...
I am trying to return each restaurant with how many 3-star reviews they have and list them using the rank column from high to low.
I know i can use count, i was thinking of count if ( review_stars=3) and then order by rank to return it... i am stuck and any help would be appreciated. Thank you.
What you are wanting to accomplish is counting how many 3 star reviews each restaurant has ..
You really are almost there -- Your original question really does contains all the answers, just written out in long-hand. Think about what you are asking:
"I need to SELECT the restaurants that have 3 star reviews and count how many there are per restaurant" -- The basic syntax you are missing is GROUP BY -- Which groups all of your results per restaurant to a single row.
SELECT restaurant, count(*) as 3_star_count from table where review_stars = '3' GROUP BY restaurant
This is a basic example. But from what you are asking .. This syntax should get you the number of 3 star reviews for each restaurant.
I would recommend that you look into SQL clauses and what they mean as #Alexis stated in an earlier comment. The WHERE clause and the GROUP BY clause (especially this one) are what you want to understand here.
Related
I know this question has been asked a lot already, but I couldn't find a solution that worked for my problem.
I have a database of books a college uses and I'm trying to write an SQL statement to display the titles of books, their course numbers, and departments. I need to order them alphabetically by the title of the book and then group them by the school division. This is what I have:
SELECT title, course_number, department
FROM books
GROUP BY school_division
ORDER BY title;
But it only prints out 3 records when I actually have 10 in total. I'm not sure how to get it to print out all 10 records?
If I get rid of the GROUP BY then it prints out all 10 records so I'm not sure what's happening.
SELECT title, course_number, department
FROM books
ORDER BY school_division,title ;
Do not use GROUP BY here.
I would appreciate your help with this task.
I have to use data from two tables:
tourist_country (tourist_id, country_id), and
tourist_age_category (tourist_id, age_category_id).
I know how to get number of tourists for each country id, and number of tourists for each age category. But what I need is the number of tourists for each country_id but with a specific age category.
I believe I'm close to my answer when joining those tables:
SELECT *
FROM tourist_age_category
JOIN tourist_country ON tourist_country.tourist_id = tourist_age_category.tourist_id
But it hasn't gotten me anywhere so I ask for help, thank you!
Not quite sure I understand what you mean, but perhaps this is what you want:
SELECT country_id, age_category_id, count(*)
FROM tourist_age_category
JOIN tourist_country ON tourist_country.tourist_id = tourist_age_category.tourist_id
GROUP BY country_id, age_category_id
I am fairly new to Databases and I am just beginning to understand the DML/queries, I have two tables, one named customer this contain customer data and one named requested_games, this contains games requested by the customers, I would like to write a query that will return the customers that have requested more than two games, so far when I run the query, I don't get the desired result, not sure if I'm doing it right.
Can anyone assist with this thanks,
Below is a snippet of the query
select customers.customer_name, wants_list.requested_game, wants_list.wantslists_id,count(wants_list.customers_ID)
from customers, wants_list
where customers.customers_ID = wants_list.customers_id
and wants_list.wantslists_id = wants_list.wantslists_id
and wants_list.requested_game > '2';
just include a HAVING clause
GROUP BY customers_ID
HAVING COUNT(*) > 2
depending on how you have your data setup you may need to do
HAVING COUNT(wants_list.requested_game) > 2
This is how I like to describe how a query works maybe itll help you visualize how the query executes :)
SELECT is making an order at a restaurant....
FROM is the menu you want to order from....
JOIN is what sections of the menu you want to include
WHERE is any customization you want to make to your order (aka no mushrooms)....
GROUP BY (and anything after) is after the order has been completed and is at your table...
GROUP BY tells your server to bring your types of food together in groups
ORDER BY is saying what dishes you want first (aka i want my entree then dessert then appetizer ).
HAVING can be used to pick out any mushrooms that were accidentally left on the plate....
etc..
I would like to write a query that will return the customers that
have requested more than two games
For this to happen you need to do the following
First you need to use GROUP BY to group the games based on customers (customers_id)
Then you need to use HAVING clause to get customers who requested more than two games
Then make this a SUBQUERY if you need more information on the customer like name
Finally you use a JOIN between customers and the sub query (temp) to display more information on the customer
Like the following query
SELECT customers.customer_id, customers.customer_name, game_count
FROM (SELECT customer_id, count(wantslists_id) AS game_count
FROM wants_list
GROUP BY customer_id
HAVING count(requested_game) > '2') temp
JOIN customers ON customers.customer_id = temp.customer_id
I have two tables
Prodline
Prodline_nbr
Prodline_name
prodline_color_code
prodline_discount
product
prod_nbr
prod_name
prod_price
prod_cost
i want to list name for each product, sale price, product line name, and color code for products whose price is no more than $200 and sort them descending order by sale price.
Here is my code
select PRODLINE_NAME,PRODLINE_COLOR_CODE,PROD_NAME, PROD_PRICE
from prodline, product
where PRODLINE_NBR in (select PRODLINE_NBR from prodline)
and prod_price<=200
order by PROD_PRICE desc;
Is this not correct? I've been told there is a better way by using some sorta join?
second attempt
select PRODLINE_NAME,PRODLINE_COLOR_CODE,PROD_NAME, PROD_PRICE
from prodline, product
where PRODLINE_NBR = PROD_NBR
and prod_price<=200
order by PROD_PRICE desc;
Could anyone comment please?
Thank you.
The second one is correct. The first one is wrong, because it just multiply two tables, i guess this is not enough to achieve your goal.
Comparing your two scripts, the only difference is a condition in the where clause:
the first uses PRODLINE_NBR in (select PRODLINE_NBR from prodline), which makes little sense, since PRODLINE_NBR is a field of prodline, of course every PRODLINE_NBR should in it. With this condition or not makes nothing different.
the second uses PRODLINE_NBR = PROD_NBR, which is a typical method to join two tables from multiplying them. If you want to connect o two tables through an equation among two fields, you need it.
Second style is the best! There are many ways to produce same outputs. but efficiency matters.
this links are good to learn about HOW JOIN and INDEX work.
http://dev.mysql.com/doc/refman/5.5/en/optimization-indexes.html
http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_selectivity
http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
We're hiring some third party Test engineers and programmers to help us with some bugs on our website. They would be working on a beta installation of our web application. The thing is that we need to give them a copy of our database, we don't want to give the entire database, its a huge database of companies. So we would want to give them a watered down version of it that has less than a fraction of the actual data -- just enough for making a proper test.
We have data in the following Schema:
COMPANIES
ID|NAME|CATEGORY|COUNTRY_ID.....
We also have a set number of categories and countries.
The thing is that we don't want the deletion to be too random, basically out of the hundreds of thousands of entries we need to give them a version that has a few hundred entries but such that, you have at least 2-3 companies for each country and category.
I'm a bit perplexed as how to do a select query with the above restriction much less delete.
It's a MySQL database we would be using here. Can this be even done in SQL or do we need to make a script in php or so?
Following select statement will select companies with first 3 id in ascending order for each category, country_id combination:
select id, name, category, country_id
from companies c1
where id in (
select id
from companies c2
where c2.category=c1.category and c2.countr_id=c1.country_id
order by id
limit 3
);
Not sure my answer will fit your needs since I am doing some assumptions that may be wrong, but you could try the following approach:
select category, country_id, min(id) id1, max(id) id2
from companies
group by country_id, category
order by country_id, category
This query only gives you 2 company ids instead of 3 and they will be the first and last id that match category and country.
Please note also I wrote this out of my mind and have no MySQL engine to test it.
Hope that helps or at least gives you a hint on how to do it.