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
Related
I've made a table with the columns for a customer name and each activity they participate in. How can I can I count the activities for each name and display it?
I've done;
SELECT Activity_Name, COUNT(*) AS 'Number_of_activities'
FROM tablename
GROUP BY Activity_Name;
which gives me each a table of each activity and how many participants in each activity but not each customer and their number of activities
Apologies for anything I've done wrong, only a couple months into coding and first time posting on stack...
Considering I don't know how your schema looks exactly, this query should be a nice representation of the idea how to do it:
SELECT customer_name, COUNT(*) AS 'Number_of_activities_per_customer'
FROM tablename
GROUP BY customer_name;
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.
I Can't make a SQL query for:
I have the following tables:
User which have two columns namely: user_id and user_name.
Group which has three columns namely : id, group_name and created_date.
Records which has five columns namely : id, group_id, user_id, record_content and record_type.
record_user which has three columns namely : user_id, record_id and liked
Now I want a query which could execute on all 4 tables something like:
select user_id, user_name, group_id, record_content
Also I need in this table - the liked field for each record based on user_id (which mean that this user is authenticated).
The thing that I try to get all messages(records) of definite group and information regarding like of each record by user which is currently reading this messages(records).
Please try the following...
SELECT User.user_id AS user_id,
User.user_name AS user_name,
Group.id AS group_id,
Group.group_name AS gorup_name,
Records.record_content AS record_content,
record_user.liked AS liked
FROM User
JOIN Records ON User.user_id = Records.user_id
JOIN Group ON Group.id = Records.group_id
JOIN record_user.record_id ON Records.id
GROUP BY User.user_id,
Group.id;
The AS's are not required, they just are useful for giving the output fields more convenient names. You can change them to pretty much whatever you want.
Please see https://www.w3schools.com/sql/sql_join.asp for more on JOIN's - look for the Venn diagram as well as read the explanation.
The GROUP BY clauses may not be needed, but you have not explained how you wish the output to be sorted and grouped, so I have chosen those as a suggestion.
Note : In the future it will help if you present us with sample data and desired output.
If you have any questions or comments, then please feel free to post a Comment accordingly.
I have two tables that are related
that table is teachers and attendance
that 2 tables, is related with field teachers.id and attendance.id_teachers
now my question is, how to displaying teachers data which is that teachers data (teachers.id) is not listed in attendance table
i hope you guys understand what i'm hoping for...
I appreciate every answer
thanks
You can try with this:
SELECT * FROM teachers WHERE id NOT IN (SELECT id_teachers FROM attendance)
IN clause is your friend here. You may want to read-up on this. Following query should give the result:
select * from teachers
where id not in (select id_teachers from attendance)
Given the following diagram:
Right now I have queries to find out how much each member has donated and also listing those donations in a list for each member. Now I want to query the donations table to get the results divided up by each organization.
Something like:
Can someone please help me with this SQL?
Assuming that you're using MySQL:
SELECT
MemberId,
OrganizationId,
SUM(Amount) AS `Amount Donated to Organization`,
COUNT(Amount) AS `Number of Donations`
FROM
Donations
GROUP BY
MemberId,
OrganizationId
;