I have a table like
Name | Image | Points | Country
-------------------------------
Bob | a.jpg | 100 | USA
Bob | b.jpg | 56 | USA
Sal | c.jpg | 87 | UK
Jim | d.jpg | 34 | UK
Bet | e.jpg | 23 | USA
Bren | f.jpg | 5 | USA
Bren | g.jpg | 15 | UK
Test | h.jpg | 10 | USA
I want to get the top 4 highest rows based on the "Points" column where the country is "USA" and removing duplicate "Names", so the desired outcome would be
Name | Image | Points | Country
-------------------------------
Bob | a.jpg | 100 | USA
Bet | e.jpg | 23 | USA
Test | h.jpg | 10 | USA
Bren | f.jpg | 5 | USA
Any help would be appreciated thank you
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(image VARCHAR(12) NOT NULL PRIMARY KEY
,name VARCHAR(12) NOT NULL
,points INT NOT NULL
,country VARCHAR(12) NOT NULL
);
INSERT INTO my_table VALUES
('a.jpg','Bob' ,100,'USA'),
('b.jpg','Bob' , 56,'USA'),
('c.jpg','Sal' , 87,'UK'),
('d.jpg','Jim' , 34,'UK'),
('e.jpg','Bet' , 23,'USA'),
('f.jpg','Bren', 5,'USA'),
('g.jpg','Bren', 15,'UK'),
('h.jpg','Test', 10,'USA');
SELECT a.*
FROM my_table a
JOIN
( SELECT name,MAX(points) points FROM my_table WHERE country = 'USA' GROUP BY name ) b
ON b.name = a.name
AND b.points = a.points
ORDER
BY points DESC
LIMIT 4;
+-------+------+--------+---------+
| image | name | points | country |
+-------+------+--------+---------+
| a.jpg | Bob | 100 | USA |
| e.jpg | Bet | 23 | USA |
| h.jpg | Test | 10 | USA |
| f.jpg | Bren | 5 | USA |
+-------+------+--------+---------+
select table.* from table join
(select Name, max(Points) mp from table where Country='USA' group by Name
order by mp desc limit 4) t
on table.Name = t.Name and table.Points = t.mp and table.Country='USA'
order by table.points desc
When a person's max point row have two record, eg bob's b.jpg is also 100, this would lead to multi rows in bobs result.
Try This:
SELECT Name,Image,MAX(points),country
FROM table_1
ORDER BY points desc
GROUP BY Name,points
LIMIT 4
Related
I have build this sql query and it works fine on phpmyadmin and as well as codeigniter;
SELECT teams.team_name,
COUNT(matches.winner) AS win
FROM matches
RIGHT JOIN teams ON teams.team_name = matches.winner
GROUP BY teams.team_name, matches.winner
ORDER BY win DESC
OUTPUT:
| team_name | win |
------------------------------
| India | 1 |
| Australia | 4 |
| England | 1 |
| South Africa | 0 |
But i need the below output: (There is another column "loser") Hence i need to apply the same query for the "loser" column similar to that i have done with "win" column.
Expected Output :
| team_name | win | loss
------------------------------
| India | 1 | 2
| Australia | 4 | 3
| England | 1 | 1
| South Africa | 0 | 2
One method would be correlated subqueries:
SELECT t.team_name,
(SELECT COUNT(*)
FROM matches m
WHERE t.team_name = m.winner
) as num_wins,
(SELECT COUNT(*)
FROM matches m
WHERE t.team_name = m.loser
) as num_loses
FROM matches t
ORDER BY num_wins DESC;
This is my table
id | name | userid | score | date |
------------------------------------------------------------
1 | john | 1 | 44 | 2013-03-2
2 | mary | 2 | 59 | 2013-03-5
3 | john | 12 | 38 | 2013-03-8
4 | elvis | 3 | 19 | 2013-02-10
5 | john | 11 | 1002 | 2013-01-11
6 | johnah | 10 | 200 | 2013-01-11
I want to show number of position of my given userid order by highest score.
if i give userid 12 in my query then it show position number 5 for user 12 depend on highest score
I think this is what you want.
SELECT COUNT(*)+1 FROM table_name WHERE userid!=12 AND
score>(SELECT score FROM table_name WHERE userid=12);
Try This :
SELECT *
FROM table_name
WHERE id =(SELECT max( userid ) FROM table_name)
I have two tables:
Students Student_Grades
V------------------------V
+----+------+ +----+------------+---------+-------+
| id | name | | id | student_id | subject | grade |
+----+------+ +----+------------+---------+-------+
| 0 | Dave | | 0 | 0 | Math | 100 |
+----+------+ +----+------------+---------+-------+
| 1 | John | | 1 | 0 | Chem | 90 |
+----+------+ +----+------------+---------+-------+
| 2 | Kate | | 2 | 0 | CompSCI | 95 |
+----+------+ +----+------------+---------+-------+
| 3 | Mimi | | 3 | 1 | ELA | 98 |
+----+------+ +----+------------+---------+-------+
| 4 | 2 | Biology | 92 |
+----+------------+---------+-------+
| 5 | 2 | Chem | 94 |
+----+------------+---------+-------+
| 6 | 2 | Math | 98 |
+----+------------+---------+-------+
| 7 | 3 | Math | 100 |
+----+------------+---------+-------+
I would like to select all subjects and grades from a random student that is enrolled in more than three subjects. (Either Dave or Kate)
Students John and Mimi would not be even considered because they are not enrolled in three subjects.
I know I can achieve this with PHP but I would like this to be done with one query to the database.
SELECT * FROM Students t JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM Students) AS x ON t.ID >= x.ID LIMIT 1
With the above query, I have selected a random student, with that I can go in and check if they have three subjects with SELECT count(subjects) FROM Students WHERE id=random_id.
If the count returned is below three, then I throw away the results and run the first query again.
How would I attempt this in one query?
This is tested and working:
SELECT *
FROM Students s
JOIN (
SELECT student_id
FROM Student_Grades
GROUP BY student_id
HAVING COUNT(*) >= 3
ORDER BY RAND()
LIMIT 1
) rs
ON rs.student_id = s.id
JOIN
Student_Grades sg
ON sg.student_id = s.id
Here's the SQL Fiddle: http://sqlfiddle.com/#!2/e5b5b/1
I have a problem with a SQL query and the resultset being returned not being what I expected.
I have these three tables that I am trying to relate.
events_detail
__________________
| ID | start_date |
| 1 | 2012-08-09 |
| 2 | 2013-02-13 |
| 3 | 2012-12-12 |
| 4 | 2013-01-21 |
| 5 | 2012-12-25 |
-------------------
where ID is the primary key
events_category_relationship
__________________________
| ID | event_id | cat_id |
| 1 | 1 | 1 |
| 2 | 2 | 4 |
| 3 | 3 | 2 |
| 4 | 4 | 2 |
| 5 | 5 | 3 |
--------------------------
where ID is primary key
events_category_detail
__________________________________
| ID | name | description |
| 1 | Europe | Kings and castles! |
| 2 | USA | Freedoms |
| 3 | China | Made in China |
| 4 | UK | Big Brother |
------------------------------------
where ID is primary key
What I need to do is grab only 1 event from each category and sorted by date of earliest appearance. So what I should expect in my result is the following
Result Set
________________________________________________________________
| e_id | start_date | c_id | category_name | category_desc |
| 1 | 2012-08-09 | 1 | Europe | Kings and castles! |
| 3 | 2012-12-12 | 2 | USA | Freedoms |
| 5 | 2012-12-25 | 3 | China | Made in China |
| 2 | 2013-02-13 | 4 | UK | Big Brother |
-----------------------------------------------------------------
My SQL query that I tried looks like this
SELECT e.id, e.start_date, c.category_name, c.category_desc
FROM events_detail e
JOIN events_category_relationship r ON r.event_id = e.id
JOIN events_category_detail c ON c.id = r.cat_id
ORDER BY date(e.start_date)
This just joins the 3 tables and returns the result in order by date. What I am stuck on is making it so that only one of each category is displayed like the desired result set above. I have tried using DISTINCT c.category_name and GROUP BY c.category_name, but none of them works.
Any help or advice will be greatly appreciated.
You will want to use a subquery to get the min(start_date) for each name and description. You will then use this result to join back to your events_details table to get the id associated with the data in the subquery:
SELECT e.id,
d.start_date,
d.name,
d.description
FROM events_detail e
INNER JOIN
(
select min(e.start_date) Start_date,
c.name,
c.description
from events_detail e
INNER JOIN events_category_relationship r
ON r.event_id = e.id
INNER JOIN events_category_detail c
ON c.id = r.cat_id
group by c.name, c.description
) d
on e.start_date = d.Start_date
ORDER BY date(e.start_date)
See SQL Fiddle with Demo
I have this table below that I need to get the number of student that get A in their exam. This is what I want to achieve from table below:
3 A's = 0 student. 2 A's = 3 student.
+--------------+------------+------+
| student_ID | kod_subjek | gred |
+--------------+------------+------+
| 746123096687 | 02 | A |
| 746123096687 | 12 | B |
| 746123096687 | 21 | A |
| 860206145454 | 12 | A |
| 860206145454 | 02 | A |
| 881012085535 | 02 | A |
| 881012085535 | 21 | A |
+--------------+------------+------+
I try with:
mysql> SELECT student_ID, COUNT(gred) FROM data_exam GROUP BY student_ID;
The output is:
+--------------+-------------+
| student_ID | COUNT(gred) |
+--------------+-------------+
| 746123096687 | 3 |
| 860206145454 | 2 |
| 881012085535 | 2 |
+--------------+-------------+
It not work. It will just count all the grade for specific student. Please help me solve this. Thank you.
SELECT a_count, COUNT(*) AS cnt
FROM
(
SELECT COUNT(*) AS a_count
FROM data_exam
WHERE gred = 'A'
GROUP BY student_id
) x
GROUP BY a_count
ORDER BY a_count
Example on SQL-Fiddle returns:
a_count cnt
2 3
You can use a subquery:
SELECT num_as, COUNT(1) AS num_students
FROM (
SELECT student_ID, COUNT(gred) AS num_as
FROM data_exam
WHERE gred = 'A'
GROUP BY student_ID
) counts_by_student
GROUP BY num_as