Wrong SQL query (beginner) - mysql

I am new in SQL, and I am learning alone on Udemy. I came across multiple questions, and I am struggling with one of them:
What is wrong with this SQL query?
SELECT sport, count(*)
FROM activities
WHERE username IN ‘tony’
GROUP BY 1;
I have two hypothesis:
1 - If the field 'sport' in activities is filled with string values, then we can't use count.
2 - the last statement should be rather:
WHERE username in:‘tony’ GROUP BY 1;
I would be happy to have your feedback on the question and learn from you!
Thanks

search IN will use with () round brackets
SELECT sport, count(*)
FROM activities
WHERE username IN ('tony')
GROUP BY 1;

Add parentheses for tony,
example: if u want to check for multiple names ('tony','stark')
SELECT sport, count(*)
FROM activities
WHERE username IN ('tony')
GROUP BY 1;

Related

ORACLE Query occurrence for an element

I'll try to explain with a generic example:
I have a table in MySQL with two columns: "Product" and "Client". For each product a client buy, a register is generated in this table.
Now I took 2 products, Stove and Refrigerator, and I want to find the client that bought both products and just them.
In this case, the expected result would be Marley, and why not Maria too? Because beyond the stove and refrigerator she also bought a chair.
How could be a MySQL ORACLE select query to give me that result?
edit: the question was wrong, it's Oracle and not MySQL, thanks Gordon Linoff
You can use aggregation. Assuming there are no duplicates in the table:
select client
from t
group by client
having sum( product in ('Stove', 'Refrigerator') ) = 2 and
count(*) = 2;
If there are duplicates, the logic is:
select client
from t
group by client
having count(distinct case when product in ('Stove', 'Refrigerator') then product end ) = 2 and
count(distinct product) = 2;
Concept Used:
Filter record for Stove and Refrigerator.
Group records by Client
Each Client should have exactly two distinct records, if yes then it should be in result set.
SELECT Client
FROM table
WHERE Product IN ('Stove', 'Refrigerator')
GROUP BY Client
HAVING COUNT(DISTINCT Product) = 2

How to count in a SQL list

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;

Selecting with multiple conditions in a Single SQL query

Table name is Student
I need the SQL query which gives names of students who are Math majors with more than 30 credits.
Thanks!
select *
from Student
where major='Math' and credits>30;
In MySQL
select firstName, lastName from Student where major='Math' and
credit>30;
Use Multiple conditions in WHERE Clause :
SELECT firstName, lastName
FROM Student
WHERE major='Math' AND credit>30;
select *
from student
where major ='Math' and credit> 30
Like others have rightly answered, the query should look like this and nothing more:
SELECT firstName, lastName FROM Student WHERE major='Math' AND credits>30;
It is worth nothing that, since you are mainly interested in just the full names of the students in the result, being specific on the name column is what is needed in the query and not SELECT ALL. This will greatly improve response time if you are working with a huge database.

how to find an amount of certain duplicates in mysql table?

So I have a table with persons.
This table connects with vehicles with manyToMany.
I need to find how many times does id appear on the table person_vehicle.
How can I do this?
Thanks everybody for asking
Here are two suggestion.
For all the names:
Gives number of times the names are repeated.
SELECT count(name) FROM persons GROUP BY name;
Specifically for the jake:
SELECT count(name) FROM persons WHERE name = "Jake";
What have you tried? Depending on the table, you can do something like:
SELECT * FROM persons WHERE first_name = 'Jake'
Which will return the number of rows with the first name of "Jake"
Alternatively, if you only want one row with the total count of occurrences:
SELECT COUNT(first_name) FROM persons WHERE first_name = 'Jake'
You're looking for GROUP BY.
A table contains multiple columns. You have to decide first which column(s) of the data that you need are in common.
Suppose I have a table address(person_name, plot_no, pin);
If I try to select, for a particulate pin, how many people are living there:
SELECT pin, COUNT(*)
FROM address
GROUP BY pin;
If I try to select, for a particulate plot_no and pin, how many people are living there:
SELECT plot_no, pin, COUNT(*)
FROM address
GROUP BY plot_no, pin;
Use group by statement on the column, on which you are looking for duplicates if the count comes more than 1, it means they are repeating.
Select count(id), name from table_name group by id HAVING count(id) > 1;
Only the duplicates records will come.
Try this:
SELECT count(name) FROM persons WHERE name = "Jake";

count number of repeating entries

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