MIN() for the minimum value of a columnMAX() for the maximum value of a columnAVG() for the mean or average value of a columnExample:
SELECT
AVG(InternetUse) AS MeanInternetUse,
MIN(InternetUse) AS MINInternet,
MAX(InternetUse) AS MAXInternet
FROM EconomicIndicators
WhereSELECT
AVG(InternetUse) AS MeanInternetUse,
MIN(InternetUse) AS MINInternet,
MAX(InternetUse) AS MAXInternet
FROM EconomicIndicators
WHERE Country = 'Solomon Islands'
GROUP BYSELECT
Country,
AVG(InternetUse) AS MeanInternetUse,
MIN(InternetUse) AS MINInternet,
MAX(InternetUse) AS MAXInternet
FROM EconomicIndicators
GROUP BY Country
WHERE for AggregationsWe cannot use
WHEREwithGROUP BYfor filtering on aggregated columns as this will throw an error
...
GROUP BY
WHERE MAX(InternetUse) > 100
Instead, we can use the
HAVING. This is how we filter on the aggregated column after using theGROUP BYclause.
GROUP BY
HAVING MAX(InternetUse) > 100
Example:
SELECT
Country,
AVG(InternetUse) AS MeanInternetUse,
MIN(GDP) AS SmallestGDP,
MAX(InternetUse) AS MAXInternetUse
FROM EconomicIndicators
GROUP BY Country
HAVING MAX(InternetUse) > 100
NULLNULL is not a number, it is not possible to use =,<,> to find or compare missing valuesNULL value, use IS NULL and IS NOT NULLSELECT
Country,
InternetUse,
Year
FROM EconomicIndicators
WHERE InternetUse IS NOT NULL
The above SQL query returns records where the value of
InternetUsecolumn is notNULL.
SELECT
Country,
InternetUse,
Year
FROM EconomicIndicators
WHERE InternetUse IS NULL
The above SQL query returns records where the value of
InternetUsecolumn isNULL.
'' can be used to find blank valuesSELECT
Country,
GDP,
Year
FROM EconomicIndicators
WHERE LEN(GDP) > 0
ISNULLSELECT
GDP,
Country,
ISNULL(Country, 'Unknown') AS NewCountry
FROM EconomicIndicators
The above SQL query returns a new column called
NewCountrywhich has all the same value as theCountryfield but the only difference being everyNULLvalue in that column is replaced by theUknownkeyword.
ISNULLSubstituting values from one column or another with ISNULL
SELECT
TradeGDPPercent,
ImportGoodPercent,
ISNULL(TradeGDPPercent,ImportGoodPercent) AS NewPercent
FROM EconomicIndicators
NULL values using COALESCE
COALESCEreturns the first non-missing value.
COALESCE(value1, value2, value3,...valuen)
value1 is NULL and value2 is not NULL then return value2value1 and value2 are NULL and value3 is not NULL then return value3 and so on.SELECT
TradeGDPPercent,
ImportGoodPercent
COALESCE(TradeGDPPercent, ImportGoodPercent, 'N/A') AS NewPercent
FROM EconomicIndicators
SELECT
Continent
CASE WHEN
Continent = 'Europe' OR Continent = 'Asia' THEN 'Eurasia'
ELSE 'Other'
END AS NewContinent
FROM EconomicIndicators
We are binning the data into discrete groups
SELECT
Country,
LifeExp,
CASE WHEN LifeExp < 30 Then 1
WHEN LifeExp > 29 AND LifeExp < 40 THEN 2
WHEN LifeExp > 39 AND LifeExp < 50 THEN 3
WHEN LifeExp > 49 AND LifeExp < 60 THEN 4
ELSE 5
END AS LifeExpGroup
FROM EconomicIndicators
WHERE Year = 2007
SELECT COUNT(*) FROM Incidents
The above SQL query returns the total number of records or rows in the
Incidentstable.
COUNT with DISTINCTSELECT COUNT(DISTINCT Country) AS Countries
FROM Incidents
The above SQL query returns the count for the number of unique country value from the
Countryfield in theIncidentstable.
GROUP BY can be used with COUNT() in the same way as the other aggregation functions such as AVG(), MIN(), MAX()ORDER BY command to sort the values.
ASC will return the smallest values first (default)DESC will return the largest values firstCOUNT with GROUP BYSELECT
COUNT(*) AS TotalRowsByCountry, Country
FROM Incidents
GROUP BY Country
It counts the rows, subtotaled by Country.
COUNT with GROUP BY and ORDER BYSELECT
COUNT(*) AS TotalRowsByCountry, Country
FROM Incidents
GROUP BY Country
ORDER BY Country ASC
It counts the rows, subtotaled by Country.
SUMSUM() provides numeric total of the values in a columnGROUP BY to get subtotals based on columns specifiedSELECT
SUM(DurationSeconds) AS TotalDuration, Country
FROM Incidents
GROUP BY Country
It calculates the sum of the
DurationSecondscolumn subtotaled byCountrycolumn from theIncidentstable.
DATEPART is used to determine what part of the date you want to calculate. Some of the common abbreviations are:
DD for DayMM for MonthYY for YearHH for HourDATEADD(): Add or subtract datetime valuesDATEDIFF(): Obtain the difference between two datetime valuesTo add or subtract a value to get a new data, we use
DATEADD()Syntax:DATEADD(DATEPART, number,date)
DATEPART: Unit of measurement (DD, MM, etc.)number: An integer value to adddate: A datetime valueIt returns a datepart value after a number has been added or substracted to a date. Syntax:
DATEDIFF(datepart, startate, enddate)
datepart: Unit of measurement (DD,MM,etc.)startdate: The starting date valueenddate: An ending datetime valueSELECT
DATEDIFF(DD, '2020-05-22', '2020-06-21') AS Difference1,
DATEDIFF(DD, '2020-07-21', '2020-06-21') AS Difference2,
The above SQL query refers the difference in day between
startdateandenddateprovided.
