SQL Resources
Return a list of India’s UNESCO heritage sites
AS
— aliasing of column namesLEFT JOIN
— returns all records in table X that have a matching record in table Y as well as records in table X that have no matching record in table YTRIM(value)
— removes leading as well as whitespace in a string
Example1
2
3
4
5
6
7
8
9
10SELECT ca.country_area_name AS `country / area`, hs.site_name AS `heritage site`, hsc.category_name AS `category`
FROM heritage_site hs
LEFT JOIN heritage_site_jurisdiction hsj
ON hs.heritage_site_id = hsj.heritage_site_id
LEFT JOIN country_area ca
ON hsj.country_area_id = ca.country_area_id
LEFT JOIN heritage_site_category hsc
ON hs.heritage_site_category_id = hsc.category_id
WHERE TRIM(ca.country_area_name) = 'India'
ORDER BY hs.site_name;
Return a count of Indian UNESCO Heritage Sites by heritage site category
COUNT(*)
— count of the number of heritage sites returned by categoryCOUNT(DISTINCT hs.heritage_site_id)
— filters out duplicate rows produced by a heritage site located in more than one country / areaGROUP BY
— follows theWHERE
clause and groups the result set by region, subregion, country / area, category
1 | SELECT ca.country_area_name AS `country / area`, hsc.category_name AS `category`, COUNT(*) AS `heritage site count` |
[caption]
The ca.country_area_name
has to be included in the GROUP BY
list, since MySQL implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them. (Reference)
Return a list of heritage sites that includes the word ‘Lake’ in the name
INNER JOIN
— returns all records in table X that have a matching record in table Y. Replacing eachINNER JOIN
with aLEFT JOIN
returns the same resultsAND
— requires that all conditions separated byAND
return trueINSTR(string, substring)
— returns the first occurrence of the substring in the string value%\[value\]%
— use of wildcards in the search string
1 | SELECT ca.country_area_name AS `country / area`, hs.site_name AS `heritage site`, |
1 | SELECT ca.country_area_name AS `country / area`, hs.site_name AS `heritage site`, |
Return list of castles, fortifications and walled cities and towns inscribed as UNESCO heritage sites
OR
— an operator that requires that any condition separated by OR return trueREGEX
— leverage regular expressions as a filter (e.g., akin to theregexp '/([a-zA-Z0-9])Castle([a-zA-Z0-9])/g')
.
1 | SELECT ca.country_area_name AS `country / area`, hs.site_name AS `heritage site`, SELECT ca.country_area_name AS `country / area`, hs.site_name AS `heritage site`, |
1 | SELECT ca.country_area_name AS `country / area`, hs.site_name AS `heritage site`, hsc.category_name AS `category` |
Return British, French, German and Dutch UNESCO heritage sites inscribed between 2010-2018
REPLACE(value, replace, replace with)
— cosmetic; substitutes the “UK” acronym for the official country name.IN(value1, value2, ...)
— an operator that permits multiple values to be referenced in the WHERE clause.BETWEEN
— — an operator that selects values within a given range, including the begin and end values.
1 | SELECT REPLACE(ca.country_area_name, 'United Kingdom of Great Britain and Northern Ireland', 'UK') AS `country / area`, hs.site_name AS `heritage site`, hsc.category_name AS `category`, hs.date_inscribed AS `date inscribed` |
Return a list of heritage site counts by region and subregion
WHERE !=
— clause condition that excludes Antarctica from the search results
1 | SELECT r.region_name AS `region`, sr.sub_region_name AS `subregion`, COUNT(DISTINCT hs.heritage_site_id) AS `heritage sites` |
Return the largest UNESCO heritage site by area (hectares) in the Caribbean
MAX(value)
— returns the maximum value, in this case, area (hectares).\G
— terminator that informs MySQL to print the result set in vertical output form.
1 | SELECT r.region_name AS `region`, sr.sub_region_name AS `subregion`, |
Return the total area (in hectares) per region that have been protected by the UNESCO heritage site designation
SUM()
— aggregate function that, in combination with the GROUP BY, provides the total area of heritage sites located in a given region.CAST(value AS datatype)
— casts area_hectares (aDOUBLE
) as aDECIMAL
datatype in order to ensure that theORDER BY
returns the result set in descending order (FLOAT
andDOUBLE
lack of precision can confound proper ordering).ROUND(number, decimals)
— rounds a number to a specified number of decimal places (in this case zero places).
1 | SELECT r.region_name AS 'region', |
Return a list of heritage sites, if any, that span regional boundaries
HAVING
— acts as a filter on the GROUP BY clause, eliminating rows that do not meet the condition specifiedGROUP_CONCAT()
— entirely decorative but handy nonetheless since it augments the raw counts with a list of the regions associated with each site.
1 | SELECT hs.site_name AS `heritage site`, |