Created
August 20, 2020 12:30
-
-
Save gbarreiro/25101a6dc1dca05939343e467ae496b4 to your computer and use it in GitHub Desktop.
MySQL cheatsheet: query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Basic query | |
SELECT * FROM Students; | |
-- Get only some columns, and filter the results | |
SELECT name, country FROM Students WHERE age>28; | |
-- Limit the number of results and sort them alphabetically | |
SELECT * FROM Students SORT BY name LIMIT 20; | |
-- Get the number of students for each country | |
SELECT COUNT(age) FROM Students GROUP BY country; | |
-- Get the average age of the students of each country, and show only those where it's lower than 30 | |
SELECT AVG(age) FROM Students GROUP BY country HAVING AVG(age)<30; | |
-- Get the name and age of all students and teachers, without skipping those who have the same name and age | |
SELECT (name, age) FROM Students UNION ALL SELECT (name, age) FROM Teachers; | |
-- Join the data from a student and her grades | |
SELECT Student.name, Grades.course, Grades.value FROM Students INNER JOIN Grades ON Students.id = Grades.student_id; | |
-- Get the names of the students with at least one A grade | |
SELECT name FROM Students WHERE id IN (SELECT student_id FROM Grades WHERE value=='A'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment