For each student, find the number of courses they take and sort
the rows in descending order. (e.g. student id, the number of
courses taken by that student)
STUDENT TABLE
| ID | name | dept_name | tot_cred |
| S0901 | Alice | Comp.Sci. | 83 |
| S0902 | Martha | Comp.Sci. | 75 |
| S0903 | Micheal | Comp.Sci. | 45 |
| S0904 | Rose | Comp.Sci. | 77 |
| S0905 | Alfie | Comp.Sci. | 91 |
| S1901 | Brad | Biology | 23 |
TAKES TABLE
| ID | course_id | sec_id | semester | year | grade
| S0901 | CS-101 | 1 | Fall | 2009 | A
| S0901 | CS-315 | 1 | Spring | 2010 | B+
| S0901 | HIS-351 | 1 | Spring | 2010 | A-
| S0901 | MTH-101 | 1 | Fall | 2009 | A
| S0901 | MTH-102 | 1 | Spring | 2009 | B+
| S0902 | CS-101 | 1 | Fall | 2009 | A
| S0902 | CS-315 | 1 | Spring | 2010 | B+
| S0902 | CS-319 | 1 | Spring | 2010 | B
| S0902 | HIS-351 | 1 | Spring | 2010 | A-
| S0902 | MTH-101 | 1 | Fall | 2009 | A
| S0902 | MTH-102 | 1 | Spring | 2009 | B+
| S1901 | CS-101 | 1 | Fall | 2009 | B+
| S1901 | CS-190 | 1 | Spring | 2009 | C
| S1901 | CS-315 | 1 | Spring | 2010 | A-
| S1901 | HIS-351 | 1 | Spring | 2010 | A-
Technically, #Marcinek's answer may not be sufficient because it omits students who take zero classes. I would use this instead:
SELECT STUDENT.ID, COUNT(TAKES.ID)
FROM STUDENT LEFT JOIN TAKES ON STUDENT.ID = TAKES.ID
GROUP BY STUDENT.ID
ORDER BY COUNT(TAKES.ID) DESC;
By using LEFT JOIN, you can capture a student whose ID does not appear in the TAKES table.
Just join and group up the result.
SELECT COUNT(*), s.id FROM student s, takes t where t.id = s.id group by s.id order by count(*) desc
Related
Am trying to learn more on mysql database and I came one problem.
I have two tables
consumables
+------------+-----------+-----------------------+------+-------------------+------+
| dod | item_code | item_description | dept | quantity_received | unit |
+------------+-----------+-----------------------+------+-------------------+------+
| 2021-12-16 | Jell001 | Petroleum Jelly | HBT | 35 | Pcs |
| 2021-12-16 | ELM001 | Consumer Control Unit | EWM | 15 | Pcs |
| 2021-12-17 | ELM002 | D3210 Contactor | EWM | 23 | Pcs |
| 2021-12-17 | ICT001 | Carburator | ICT | 23 | Pcs |
| 2021-12-17 | ICT001 | Carburator | ICT | 23 | Pcs |
| 2021-12-18 | ELM001 | Consumer Control Unit | EWM | 15 | Pcs |
+------------+-----------+-----------------------+------+-------------------+------+
issue_consumables table
+----+----------+------------+--------------+-----------+-----------------+------+
| id | username | doe | issued_to | item_code | quantity_issued | unit |
+----+----------+------------+--------------+-----------+-----------------+------+
| 1 | STAMP | 2021-12-18 | John Doe | ELM001 | 4 | Pcs |
| 2 | STAMP | 2021-12-18 | John Doe | ELM002 | 5 | Pcs |
| 3 | STAMP | 2021-12-18 | John Doe | ICT001 | 35 | Pcs |
| 4 | STAMP | 2021-12-15 | Jessy Jesica | Jell001 | 20 | Pcs |
+----+----------+------------+--------------+-----------+-----------------+------+
My desired Results
+----+-----------+------------+--------------+-----------------------+-----------------+
| id | item_code | date1 | issued_to | item_description | quantity_issued |
+----+-----------+------------+--------------+-----------------------+-----------------+
| 4 | Jell001 | 15-12-2021 | Jessy Jesica | Petroleum Jelly | 20 |
| 3 | ICT001 | 18-12-2021 | John Doe | Carburator | 35 |
| 2 | ELM002 | 18-12-2021 | John Doe | Consumer Control Unit | 5 |
| 1 | ELM001 | 18-12-2021 | John Doe | Petroleum Jelly | 4 |
+----+-----------+------------+--------------+-----------------------+-----------------+
My Query is
SELECT
issue_consumables.id,
issue_consumables.item_code,
DATE_FORMAT(issue_consumables.doe,'%d-%m-%Y')as date1,
issue_consumables.issued_to,
consumables.item_description,
issue_consumables.quantity_issued
FROM consumables
RIGHT JOIN issue_consumables ON consumables.item_code = issue_consumables.item_code
ORDER BY issue_consumables.id DESC
the Results am getting
+----+-----------+------------+--------------+-----------------------+-----------------+
| id | item_code | date1 | issued_to | item_description | quantity_issued |
+----+-----------+------------+--------------+-----------------------+-----------------+
| 4 | Jell001 | 15-12-2021 | Jessy Jesica | Petroleum Jelly | 20 |
| 3 | ICT001 | 18-12-2021 | John Doe | Carburator | 35 |
| 3 | ICT001 | 18-12-2021 | John Doe | Carburator | 35 |
| 2 | ELM002 | 18-12-2021 | John Doe | D3210 Contactor | 5 |
| 1 | ELM001 | 18-12-2021 | John Doe | Consumer Control Unit | 4 |
| 1 | ELM001 | 18-12-2021 | John Doe | Consumer Control Unit | 4 |
+----+-----------+------------+--------------+-----------------------+-----------------+
where am I doing Wrong to get the desired results
You need to have a set of unique item_code and its description in order to achieve the result.
Something like:
select issue_consumables.id,
issue_consumables.item_code,
DATE_FORMAT(issue_consumables.doe,'%d-%m-%Y')as date1,
issue_consumables.issued_to,
consumables.item_description,
issue_consumables.quantity_issued
FROM issue_consumables
join ( select distinct item_code, item_description from consumables ) consumables on consumables.item_code = issue_consumables.item_code
to include quantity_received
select issue_consumables.id,
issue_consumables.item_code,
DATE_FORMAT(issue_consumables.doe,'%d-%m-%Y')as date1,
issue_consumables.issued_to,
consumables.item_description,
issue_consumables.quantity_issued,
consumables.quantity_received
FROM issue_consumables
join ( select item_code, item_description, sum(quantity_received) quantity_received from consumables group by item_code, item_description ) consumables on consumables.item_code = issue_consumables.item_code
I am learning SQL and DB, I need to make the following query, I need to make a query that finds the dates that there were more car crashes and list the names of the people who were involved in these car crashes
person
| name | id_person |
|--------|------------|
| Oliver | 000000001 |
| Harry | 000000002 |
| Jacob | 000000003 |
| Maria | 000000004 |
| Jack | 000000005 |
participated
| id_person | num_crash | cost_damage |
|------------|-------------|---------------|
| 00000001 | 11111101 | 200 |
| 00000002 | 11111102 | 120 |
| 00000003 | 11111102 | 120 |
| 00000004 | 11111103 | 400 |
| 00000005 | 11111104 | 300 |
| 00000002 | 11111105 | 280 |
| 00000005 | 11111106 | 260 |
crash
| num_crash | date_crash | crash_scene |
|-------------|--------------|-------------|
| 11111101 | 2020/04/28 | bairro 4 |
| 11111102 | 2020/05/01 | bairro 1 |
| 11111103 | 2020/05/01 | bairro 2 |
| 11111104 | 2020/05/04 | bairro 3 |
| 11111105 | 2020/05/04 | bairro 1 |
| 11111106 | 2020/05/04 | bairro 3 |
output example
| data_crash | num_crash | name |
|--------------|-------------|-------|
| 2020/05/04 | 11111104 | Jack |
| 2020/05/04 | 11111105 | Harry |
| 2020/05/04 | 11111106 | Jack |
| 2020/05/01 | 11111102 | Harry |
| 2020/05/01 | 11111102 | Jacob |
| 2020/05/01 | 11111103 | Maria |
This is my sql query
CREATE VIEW vwfrequencedatecrash AS
SELECT date_crash, num_crash, crash_scene, ROW_NUMBER() OVER (PARTITION
BY date_crash ORDER BY date_crash) AS frequence
FROM crash
ORDER BY frequence DESC;
CREATE VIEW vwmorefrequencedate AS
SELECT date_crash, num_crash, crash_scene, frequence
FROM vwfrequencedatecrash
WHERE frequence > 1;
SELECT vw.date_crash, pa.num_crash, p.name
FROM vwmorefrequencedate vw
JOIN crash c ON c.date_crash = vw.date_crash
JOIN participated pa ON c.num_crash = pa.num_crash
JOIN person p ON pa.id_person = p.id_person
ORDER BY vw.frequence DESC, c.date_crash;
how can I improve this query?
I have the following structure :
Table Author :
idAuthor,
Name
+----------+-------+
| idAuthor | Name |
+----------+-------+
| 1 | Renee |
| 2 | John |
| 3 | Bob |
| 4 | Bryan |
+----------+-------+
Table Publication:
idPublication,
Title,
Type,
Date,
Journal,
Conference
+---------------+--------------+------+-------------+------------+-----------+
| idPublication | Title | Date | Type | Conference | Journal |
+---------------+--------------+------+-------------+------------+-----------+
| 1 | Flower thing | 2008 | book | NULL | NULL |
| 2 | Bees | 2009 | article | NULL | Le Monde |
| 3 | Wasps | 2010 | inproceding | KDD | NULL |
| 4 | Whales | 2010 | inproceding | DPC | NULL |
| 5 | Lyon | 2011 | article | NULL | Le Figaro |
| 6 | Plants | 2012 | book | NULL | NULL |
| 7 | Walls | 2009 | proceeding | KDD | NULL |
| 8 | Juices | 2010 | proceeding | KDD | NULL |
| 9 | Fruits | 2010 | proceeding | DPC | NULL |
| 10 | Computers | 2010 | inproceding | DPC | NULL |
| 11 | Phones | 2010 | inproceding | DPC | NULL |
| 12 | Creams | 2010 | proceeding | DPC | NULL |
| 13 | Love | 2010 | proceeding | DPC | NULL |
+---------------+--------------+------+-------------+------------+-----------+
Table author_has_publication :
Author_idAuthor,
Publication_idPublication
+-----------------+---------------------------+
| Author_idAuthor | Publication_idPublication |
+-----------------+---------------------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 1 | 5 |
| 2 | 5 |
| 3 | 5 |
| 3 | 6 |
| 4 | 7 |
| 4 | 8 |
| 4 | 9 |
| 4 | 10 |
| 3 | 11 |
| 3 | 12 |
| 2 | 13 |
+-----------------+---------------------------+
I want to obtain the list of all authors having published at least 2 times at conference DPC in 2010.
I achieved to get the list of autors that have published something, and the number of publication for each, but I can't get my 'at least 2' factor.
My following query
SELECT author.name, COUNT(name) FROM author INNER JOIN author_has_publication ON author.idAuthor=author_has_publication.Author_idAuthor INNER JOIN publication ON author_has_publication.Publication_idPublication=publication.idPublication AND publication.date=2010 AND publication.conference='DPC'GROUP BY author.name;
returns the following result (which is good)
+-------+-------------+
| name | COUNT(name) |
+-------+-------------+
| Bob | 2 |
| Bryan | 3 |
| John | 1 |
+-------+-------------+
but when I try to select only the one with a count(name)>=2, i got an error.
I tried this query :
SELECT author.name, COUNT(name) FROM author INNER JOIN author_has_publication ON author.idAuthor=author_has_publication.Author_idAuthor INNER JOIN publication ON author_has_publication.Publication_idPublication=publication.idPublication AND publication.date=2010 AND publication.conference='DPC'GROUP BY author.name WHERE COUNT(name)>=2;
When you use aggregation funcion you can filter with a proper operator named HAVING
Having worok on the result of the query (then pn the aggrgated result like count() ) instead of where that work on the original value of the tables rows
SELECT author.name, COUNT(name)
FROM author INNER JOIN author_has_publication
ON author.idAuthor=author_has_publication.Author_idAuthor
INNER JOIN publication
ON author_has_publication.Publication_idPublication=publication.idPublication
AND publication.date=2010 AND publication.conference='DPC'
GROUP BY author.name
HAVING COUNT(name)>=2;
I'm working on a MySQL database and I need to query the database and find out the users with more than one order. I tried using COUNT() but I cannot get it right. Can you please explain the correct way to do this?.
Here are my tables:
User
+-------------+----------+------------------+------------+
| userID | fName | email | phone |
+-------------+----------+------------------+------------+
| adele012 | Adele | aash#gmail.com | 0123948498 |
| ana022 | Anna | ashow#gmail.com | 0228374847 |
| david2012 | David | north#gmail.com | 902849302 |
| jefAlan | Jeffery | jefal#gmail.com | 0338473837 |
| josquein | Joseph | jquein#gmail,com | 0098374678 |
| jweiz | John | jwei#gmail.com | 3294783784 |
| jwick123 | John | jwik#gmail.com | 0998398390 |
| kenwipp | Kenneth | kwip#gmail.com | 0112938394 |
| mathCler | Maththew | matc#gmail.com | 0238927483 |
| natalij2012 | Natalie | nj#gmail.com | 1129093210 |
+-------------+----------+------------------+------------+
Orders
+---------+------------+-------------+-------------+
| orderID | date | User_userID | orderStatus |
+---------+------------+-------------+-------------+
| 1 | 2012-01-10 | david2012 | Delivered |
| 2 | 2012-01-15 | jweiz | Delivered |
| 3 | 2013-08-15 | david2012 | Delivered |
| 4 | 2013-03-15 | natalij2012 | Delivered |
| 5 | 2014-03-04 | josquein | Delivered |
| 6 | 2014-01-15 | jweiz | Delivered |
| 7 | 2014-02-15 | josquein | Delivered |
| 8 | 2015-10-12 | jwick123 | Delivered |
| 9 | 2015-02-20 | ana022 | Delivered |
| 10 | 2015-11-20 | kenwipp | Processed |
+---------+------------+-------------+-------------+
select user_userID, count(*) as orders_count from orders
group by user_userID having orders_count > 1
if you want additional data from your users table, you can do:
select * from user where user_id in (
select user_userID as orders_count from orders
group by user_userID having orders_count > 1
)
I have three tables:
Person
+--------+-----------+
| fName | lName |
+--------+-----------+
| Paul | McCartney |
| John | Lennon |
| Jon | Stewart |
| Daniel | Tosh |
| Steven | Colbert |
| Pink | Floyd |
| The | Beatles |
| Arcade | Fire |
| First | Last |
| Andrew | Bird |
+--------+-----------+
Publication
+----+---------------------------------------+------+-----------+---------+
| id | title | year | pageStart | pageEnd |
+----+---------------------------------------+------+-----------+---------+
| 9 | The Dark Side of the Moon | 1973 | 0 | 0 |
| 10 | Piper At The Gates of Dawn | 1967 | 0 | 0 |
| 11 | Sgt. Pepper's Lonely Hearts Band Club | 1967 | 0 | 0 |
| 12 | Happy Thoughts | 2007 | 0 | 60 |
| 13 | Wish You Were Here | 1975 | 0 | 0 |
| 14 | Funeral | 2004 | 0 | 0 |
+----+---------------------------------------+------+-----------+---------+
Person_Publication
+-----------+----------------+--------+---------------+
| person_id | publication_id | editor | author_number |
+-----------+----------------+--------+---------------+
| 11 | 11 | 0 | 1 |
| 12 | 11 | 0 | 1 |
| 16 | 9 | 0 | 1 |
| 17 | 11 | 0 | 1 |
+-----------+----------------+--------+---------------+
I'm trying to select all authors of a certain publication using the following query:
SELECT fName , lName
FROM Publication , Person, Person_Publication
WHERE Person.id = Person_Publication.person_id
AND Person_Publication.publication_id = 11;
But the results I get are always duplicates (always 6x for some reason). The results:
+-------+-----------+
| fName | lName |
+-------+-----------+
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
+-------+-----------+
18 rows in set (0.03 sec)
Can somebody please tell me why this is happening and how to fix this?
You are getting 6x your results, exactly one for each Publication row.
Remove your Publication from your FROM clause:
SELECT fName , lName
FROM Person, Person_Publication
WHERE Person.id = Person_Publication.person_id
AND Person_Publication.publication_id = 11;
You are including three tables in your query:
FROM Publication, Person, Person_Publication
but you have only one join condition:
WHERE Person.id = Person_Publication.person_id
You end up with a cartesian product between Publication and Person JOIN Person_Publication
Add the following condition to your WHERE block:
AND Publication.id = Person_Publication.publication.id
A perfect example of why the explicit JOIN syntax is prefered. With the following syntax:
SELECT fName, lName
FROM Publication
JOIN Person_Publication ON Person_Publication.publication.id = Publication.id
JOIN Person ON Person.id = Person_Publication.person_id
WHERE Person_Publication.publication_id = 11;
.. such a mistake simply cannot happen.