Skip to content

Instantly share code, notes, and snippets.

@quachngocxuan
Last active December 18, 2017 02:19
Show Gist options
  • Save quachngocxuan/58772e7c71e9768b2ec4bb88a41ff191 to your computer and use it in GitHub Desktop.
Save quachngocxuan/58772e7c71e9768b2ec4bb88a41ff191 to your computer and use it in GitHub Desktop.
Learn enough to be dangerous - SQL

Learn enough to be dangerous - SQL

This SQL here is for MySQL.

Use regular expression in query

Query the list of CITY names from the table STATION(id, city, longitude, latitude) which have vowels as both their first and last characters. The result cannot contain duplicates. Ref: stackoverflow Ref: Hackerrank

SELECT DISTINCT city
FROM   station
WHERE  city RLIKE '^[aeiouAEIOU].*[aeiouAEIOU]$'
SELECT DISTINCT city
FROM   station
WHERE  city NOT RLIKE '^[aeiouAEIOU]'

Order by a part of a field

SELECT                NAME
FROM                  STUDENTS
WHERE                 MARKS > 75
ORDER BY              SUBSTRING(NAME, -3), ID ASC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment