Count how many different countries are in the table - mysql

I have a table like this:
ID country
-------------
1 US
2 Japan
3 China
4 US
5 China
How can one query the table, so that it returns how many different countries are in the table (i.e 3)?

The following SQL query will result in counting the number of unique countries in your table.
select count(distinct country)
from YourTable

What you need is to select the amount of unique countries. This is done by selecting all country entries, which are then grouped so there is only one country entry per country name.
SELECT count(country) from countrytable group by country;
This is basically the same as Andomars answer.

you can use this
SELECT country, COUNT( id )
FROM [table_name] GROUP BY country
LIMIT 0 , 30
Output :
country count(id)
---------------------
US 2
Japan 1
china 2

SELECT count(distinct country) from countrytable

select count(distinct country)
from table1
SELECT count( DISTINCT country )
FROM table1 c
WHERE c.item_id = (
SELECT main_id
FROM table2
WHERE main_id = c.item_id )

select count(distinct country)
from [TableName]

Related

I want select all duplicate records from my table on the basis of student name and father name in mysql database

select student_name
from thewings_clients_temp
where concat(student_name,father_name) IN (
select concat(student_name,father_name) from thewings_clients_temp group by student_name HAVING COUNT(concat(student_name,father_name)) > 1
)
This will give you the students whose name is repeated more than once.
SELECT student_name,
father_name,
COUNT(*)
FROM thewings_clients_temp
GROUP BY student_name, father_name
HAVING COUNT(*) > 1

MySQL - SELECT Most occuring value (with several same occurrences)

I have been looking for a way to do a MySQL select to get all the most occuring values. All the solutions i have found where with using LIMIT 1, but this doesn't help if there are more than one value that occur the same amount of times. E.g:
customer
ID
FirstName
1
Bob
2
Tom
3
Bob
4
Robert
5
Tom
6
Timothy
The Select for most occuring FirstNames should result in:
FirstName
Bob
Tom
Since both occur twice.
I have tried the following:
SELECT FirstName FROM (
SELECT FirstName, COUNT(FirstName) as counter FROM customer
GROUP BY FirstName
HAVING counter = MAX(counter)
) s
But this doesn't seem to work, i would really appreciate a nudge in the right direction.
There can be different ways for doing this
you can try
1st
SELECT FIRSTNAME
FROM customer
GROUP BY FirstName Having count(FirstName) = (
SELECT COUNT(FirstName) FROM customer GROUP BY FirstName ORDER BY 1
DESC LIMIT 1);
2nd
with cte as
(
SELECT COUNT(FirstName) MaxCounter
FROM customer
Group By FirstName ORDER BY COUNT(FirstName) DESC LIMIT 1
)
SELECT c.FirstName
From customer c
Group BY FirstName
HAVING COUNT(FirstName) = (SELECT MaxCounter FROM cte)

how to write a generic SQL select statement to select a person who belong to all city

I have to write a generic sql query for following situation:
city_id, person_id
0 0
1 1
0 2
1 3
0 3
0 4
1 4
1 5
0 6
0 7
I have to select only those persons who belongs to both cities 0 and 1, if new city and new person will enter then select only those person how belong to all city.
something like following:
SELECT person_id
FROM table
GROUP BY person_id
HAVING COUNT(*) > (
(SELECT COUNT(DISTINCT city_id) FROM table
)-1);
I hope it helps others.
To find people belonging to both City 0 and City 1, if those are the only two cities you're looking at:
SELECT person_id,
COUNT(*)
FROM table
GROUP BY person_id
HAVING COUNT(*) > 1
This will show you all the person_id values that have more than one record (such as where they belong to more than one city.)
You can further refine this by city if you have more than two cities:
SELECT person_id,
COUNT(*)
FROM table
WHERE city_id IN (0, 1) -- or any city configuration you need.
GROUP BY person_id
HAVING COUNT(*) > 1
This query doesn't make any assumption about how many cities are in the database.
select person_id from T
where city_id in (0, 1)
group by person_id
having count(distinct city_id) = 2 /* distinct isn't necessary if city_id is guaranteed unique per person */
If your city_id is auto increment. Something like this would work too.
SELECT person_id
FROM table
GROUP BY person_id
HAVING COUNT(*) > (SELECT MAX(city_id) FROM cities)+1;

Mysql: which pizza prefer client

I have table with this data :
1. John | seafood pizza
2. Mike | pepperoni pizza
3. Mike | pepperoni pizza
4. John | original pizza
5. Mike | original pizza
6. John | seafood pizza
7. John | pepperoni pizza
....
How can I write a query that give me result such this:
John | seafood pizza
Mike | pepperoni pizza
If client have same quantity for many pizzas, the result may be any pizza's name.
Let's take it step by step
the following query will give you number of times each client ordered each kind of pizza
SELECT name, pizza, COUNT(*) AS cnt
FROM yourTable
GROUP BY name, pizza
So how do we get the most often ordered pizza from that? We must first know, what is the highest number of pizza ordered by each client
SELECT name, MAX(cnt) AS cnt FROM (
SELECT name, pizza, COUNT(*) AS cnt
FROM yourTable
GROUP BY name, pizza
) AS subquery GROUP BY name
then use this number to select actual pizza name
SELECT name, pizza, COUNT(*) AS cnt
FROM yourTable AS t
CROSS JOIN (
SELECT name, MAX(cnt) AS cnt FROM (
SELECT name, pizza, COUNT(*) AS cnt
FROM yourTable
GROUP BY name, pizza
) AS subquery GROUP BY name
) AS sq
USING(name,cnt)
GROUP BY name, pizza
I'd go with grouping the counts by user and pizza then using this as a datasource for a query which finds the groupwise maximum (using the max concat trick). It's not hard:
SELECT user, SUBSTRING(MAX(CONCAT(LPAD(freq, 6, '0'),pizza)),7)
FROM
(SELECT user, pizza, COUNT(*) AS freq
FROM user_likes
GROUP BY user, pizza) ilv
GROUP BY user
Let's have a simple solution :
First of all, we have two columns,name and food.
You can list of favorite customer's food easily :
CREATE TEMPORARY TABLE tbltemp AS (
SELECT name, food, COUNT( * ) c
FROM food
GROUP BY name, food
);
Now we have all of foods by count.
Then you can order it by count descending :
SELECT *
FROM tbltemp
ORDER BY c DESC
Now you have a list that describes your customer's favorite.
UPDATE :
Instead of second query ,replace this one :
SELECT tbl1 . *
FROM (
SELECT name, food, COUNT( * ) c
FROM food
GROUP BY name, food
) AS tbl1, (
SELECT name, food, COUNT( * ) c
FROM food
GROUP BY name, food
) AS tbl2
WHERE tbl1.c = tbl2.c
AND tbl2.name = tbl1.name
AND tbl2.food = tbl1.food
AND tbl1.c = (
SELECT c
FROM tbltemp
ORDER BY c DESC
LIMIT 1 )
This query result is your question goal.

How to get distinct record from mysql table?

I have a table student like this
id | name | zip
1 | abc | 1234
2 | xyz | 4321
3 | asd | 1234
I want to get all records but zip code should not be repeated. So In case of above table records, record No 1 and 2 should be fetched. Record No. 3 will not be fetched because it has a zip code which is already in record No. 1
SELECT DISTINCT fieldName FROM tableName;
The following query will only select distinct 'zip' field.
SELECT DISTINCT zip FROM student;
SELECT * FROM tableName GROUP BY fieldName;
The following query will select all fields along with distinct zip field.
SELECT * FROM student GROUP BY zip;
TRY
SELECT DISTINCT(zip),id,name FROM student;
OR
SELECT * FROM student GROUP BY zip;
Altough in MySQL you can get away with:
SELECT *
FROM student
GROUP BY zip
I would choose:
SELECT *
FROM student t
JOIN
( SELECT MIN(id) AS minid
FROM student
GROUP BY zip
) AS grp
ON grp.minid = t.id
Since presumably the other columns are of some interest....
SELECT y.*
FROM yourTable y,
(SELECT MIN(y2.id)
FROM yourTable y2
GROUP BY y2.zip) ilv
WHERE ilv.id=y.id;
(or you could use the max-concat trick)
update
Oracle have now removed the max concat trick from the linked page - but it is described elsewhere on the internet
Try Using
Select Distinct(zip),id,name group by zip;
Is there any problem if I use as this below?
select distinct zip,name,id from student;
select id, name, distinct(zip) from student;