diff --git a/tech/sql.wiki b/tech/sql.wiki index ec3b5cb..950d8ca 100644 --- a/tech/sql.wiki +++ b/tech/sql.wiki @@ -34,7 +34,68 @@ will return all bands with the string 'light' in their name. === DISTINCT === -`DISTINCT` filters results by only returning unique values in a column +`DISTINCT` filters results by only returning unique values of a column. +Therefore, we can only be used to return a single column. + +{{{ +SELECT DISTINCT nationality FROM users; + }}} + +== Presentation == + +=== Order by === + +`ORDER BY` allows us to descide how the query will be shown. For example, + +{{{ +SELECT Firstname, Lastname +FROM users +ORDER BY Lastname [SORT]; + }}} + +where `[SORT]` is + +* DESC + * descending order +* ASC + * ascending order + +=== CASE === + +Case is a statement block where we can choose exactly what text is returned +based on values in the database. + +{{{ +SELECT Name, +CASE + WHEN Country = 'USA' THEN 'North America' + WHEN Country = 'UK' THEN 'Europe' + ELSE 'Wolrd' +END +FROM Users; + }}} + +=== LIMIT === + +`LIMIT` allows us to retstrict the number or rows returned. + +`SELECT * FROM users ORDER BY name DESC LIMIT 1` + +=== COUNT === + +If we want just the number of rows, we can use count. + +`SELECT COUNT(*) FROM users WHERE dob > '2000-01-01'` + +=== math functions === + +If we want to sum the values in ONE column we can use sum + +`SELECT SUM(cash_balance) FROM users` + +If we want to find an average, + +`SELECT AVG(price) FROM products` == Views ==