Skip to content

Instantly share code, notes, and snippets.

@IvikGH
Last active December 12, 2018 23:04
Show Gist options
  • Save IvikGH/73cfd77fa9f08a0229eefc0c8b4a3b23 to your computer and use it in GitHub Desktop.
Save IvikGH/73cfd77fa9f08a0229eefc0c8b4a3b23 to your computer and use it in GitHub Desktop.
test sql queries
# get all students last_name, alphabetically ordered, not repeating
SELECT DISTINCT last_name
FROM students
ORDER BY last_name
# get the count of all students in each groups, order by groups names descending
SELECT groups.name,
Count(students.id)
FROM groups
LEFT JOIN students
ON groups.id = students.group_id
GROUP BY groups.name
ORDER BY groups.name DESC
# get the list of students with duplicate first_names. Order alphabetically
SELECT DISTINCT ls.first_name,
ls.last_name
FROM students AS ls
INNER JOIN students AS rs
ON ( ls.first_name = rs.first_name
AND ls.id != rs.id )
ORDER BY ls.first_name
# get list of all groups names, ordered by sum of max rating of students
SELECT groups.NAME, coalesce(sum(students.rating), 0) as gr_rating
FROM groups
LEFT JOIN students # может быть INNER JOIN в зависимости от конкретизации задачи
ON groups.id = students.group_id
GROUP BY groups.NAME
ORDER BY gr_rating
# get list of all students with first_name ‘F’
SELECT *
FROM students
where SUBSTRING(first_name, 1, 1) = 'F'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment