Amisha Dalal PGT (Computer Science) SQL Practice: Here Here Here
Amisha Dalal PGT (Computer Science) SQL Practice: Here Here Here
Aggregate functions
37. Select but without duplicates: SELECT distinct name, email, acception FROM owners WHERE
acception = 1 AND date >= 2015-01-01 00:00:00
38. Calculate total number of records: SELECT SUM([column]) FROM [table];
39. Count total number of [column] and group by [category-column]: SELECT [category-column],
SUM([column]) FROM [table] GROUP BY [category-column];
40. Get largest value in [column]: SELECT MAX([column]) FROM [table];
41. Get smallest value: SELECT MIN([column]) FROM [table];
42. Get average value: SELECT AVG([column]) FROM [table];
43. Get rounded average value and group by [category-column]: SELECT [category-column],
ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];
Multiple tables
44. Select from multiple tables: SELECT [table1].[column], [table1].[another-column],
[table2].[column] FROM [table1], [table2];
45. Combine rows from different tables: SELECT * FROM [table1] INNER JOIN [table2] ON
[table1].[column] = [table2].[column];
46. Combine rows from different tables but do not require the join condition: SELECT *
FROM [table1] LEFT OUTER JOIN [table2] ON [table1].[column] = [table2].[column]; (The left
table is the first table that appears in the statement.)
47. Rename column or table using an alias: SELECT [table1].[column] AS '[value]',
[table2].[column] AS '[value]' FROM [table1], [table2];
Users functions
48. List all users: SELECT User,Host FROM mysql.user;
49. Create new user: CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
50. Grant ALL access to user for * tables: GRANT ALL ON database.* TO 'user'@'localhost';
Find out the IP Address of the Mysql Host
51. SHOW VARIABLES WHERE Variable_name = 'hostname'; (source)