0% found this document useful (0 votes)
58 views2 pages

Amisha Dalal PGT (Computer Science) SQL Practice: Here Here Here

This document provides 51 SQL commands for working with databases, tables, records, users and access permissions. It covers basic queries like selecting, inserting, updating and deleting records. It also includes commands for creating databases and tables, setting primary keys, joining tables, and using aggregate functions. Administrative tasks like creating users and granting permissions are also demonstrated.

Uploaded by

Amisha Dalal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
58 views2 pages

Amisha Dalal PGT (Computer Science) SQL Practice: Here Here Here

This document provides 51 SQL commands for working with databases, tables, records, users and access permissions. It covers basic queries like selecting, inserting, updating and deleting records. It also includes commands for creating databases and tables, setting primary keys, joining tables, and using aggregate functions. Administrative tasks like creating users and granting permissions are also demonstrated.

Uploaded by

Amisha Dalal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

AMISHA DALAL PGT (COMPUTER SCIENCE)SQL PRACTICE

1. Show all databases: show databases;


2. Access database: mysql -u [username] -p [database] (will prompt for password)
3. Create new database: create database [database];
4. Select database: use [database];
5. Determine what database is in use: select database();
6. Show all tables: show tables;
7. Show table structure: describe [table];
8. List all indexes on a table: show index from [table];
9. Create new table with columns: CREATE TABLE [table] ([column] VARCHAR(120), [another-
column] DATETIME);
10. Adding a column: ALTER TABLE [table] ADD COLUMN [column] VARCHAR(120);
11. Adding a column with an unique, auto-incrementing ID: ALTER TABLE [table] ADD
COLUMN [column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;
12. Inserting a record: INSERT INTO [table] ([column], [column]) VALUES ('[value]', [value]');
13. MySQL function for datetime input: NOW()
14. Selecting records: SELECT * FROM [table];
15. Explain records: EXPLAIN SELECT * FROM [table];
16. Selecting parts of records: SELECT [column], [another-column] FROM [table];
17. Counting records: SELECT COUNT([column]) FROM [table];
18. Counting and selecting grouped records: SELECT *, (SELECT COUNT([column]) FROM
[table]) AS count FROM [table] GROUP BY [column];
19. Selecting specific records: SELECT * FROM [table] WHERE [column] =
[value]; (Selectors: <, >, !=; combine multiple selectors with AND, OR)
20. Select records containing [value]: SELECT * FROM [table] WHERE [column] LIKE '%[value]%';
21. Select records starting with [value]: SELECT * FROM [table] WHERE [column] LIKE '[value]%';
22. Select records starting with val and ending with ue: SELECT * FROM [table] WHERE
[column] LIKE '[val_ue]';
23. Select a range: SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];
24. Select with custom order and only limit: SELECT * FROM [table] WHERE [column] ORDER BY
[column] ASC LIMIT [value];(Order: DESC, ASC)
25. Updating records: UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];
26. Deleting records: DELETE FROM [table] WHERE [column] = [value];
27. Delete all records from a table (without dropping the table itself): DELETE FROM
[table]; (This also resets the incrementing counter for auto generated columns like an
id column.)
28. Delete all records in a table: truncate table [table];
29. Removing table columns: ALTER TABLE [table] DROP COLUMN [column];
30. Deleting tables: DROP TABLE [table];
31. Deleting databases: DROP DATABASE [database];
32. Custom column output names: SELECT [column] AS [custom-column] FROM [table];
33. Export a database dump (more info here): mysqldump -u [username] -p [database] >
db_backup.sql
34. Use --lock-tables=false option for locked tables (more info here).
35. Import a database dump (more info here): mysql -u [username] -p -h localhost [database] <
db_backup.sql
36. Logout: exit;
AMISHA DALAL PGT (COMPUTER SCIENCE)SQL PRACTICE

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)

You might also like