0% found this document useful (0 votes)
3 views

All SQL Commands List with Example - (AtoZ Library)

The document provides a comprehensive list of SQL commands along with their descriptions and syntax. It covers a variety of commands including data retrieval, manipulation, and database management. Each command is numbered and includes examples for clarity.

Uploaded by

nak6ai6666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

All SQL Commands List with Example - (AtoZ Library)

The document provides a comprehensive list of SQL commands along with their descriptions and syntax. It covers a variety of commands including data retrieval, manipulation, and database management. Each command is numbered and includes examples for clarity.

Uploaded by

nak6ai6666
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

AtoZ Library Free Resources

All SQL Commands List with Example


Downloaded From atozibrary.in (Visit For More)
Sl No Command Description Syntax
SELECT column1, column2 FROM table_name WHERE
1 SELECT Retrieves data from one or more tables.
condition;
INSERT INTO table_name (column1, column2)
2 INSERT Adds new records to a table.
VALUES (value1, value2);
UPDATE table_name SET column1 = value1, column2
3 UPDATE Modifies existing records in a table.
= value2 WHERE condition;
4 DELETE Removes records from a table. DELETE FROM table_name WHERE condition;
Creates a new database object such as a table or CREATE TABLE table_name (column1 datatype,
5 CREATE
index. column2 datatype);
Modifies an existing database object, such as a table ALTER TABLE table_name ADD column_name
6 ALTER
structure. datatype;
7 DROP Deletes an existing database object. DROP TABLE table_name;
Removes all records from a table, but not the table
8 TRUNCATE TRUNCATE TABLE table_name;
itself.
ALTER TABLE old_table_name RENAME TO
9 RENAME Renames a database object.
new_table_name;
10 GRANT Gives a user permission to access a database object. GRANT permission ON object TO user;
11 REVOKE Removes access permissions from a user. REVOKE permission ON object FROM user;
Saves all changes made during the current
12 COMMIT COMMIT;
transaction.
Undoes changes made during the current
13 ROLLBACK ROLLBACK;
transaction.
Sets a save point within a transaction to which you
14 SAVEPOINT SAVEPOINT savepoint_name;
can roll back.
SET TRANSACTION ISOLATION LEVEL READ
15 SET TRANSACTION Sets the properties of a transaction.
COMMITTED;
16 SELECT DISTINCT Retrieves unique values from a column. SELECT DISTINCT column_name FROM table_name;
Counts the number of records that meet a specified SELECT COUNT(column_name) FROM table_name
17 COUNT
condition. WHERE condition;
SELECT SUM(column_name) FROM table_name
18 SUM Calculates the sum of values in a column.
WHERE condition;
SELECT AVG(column_name) FROM table_name
19 AVG Calculates the average of values in a column.
WHERE condition;
SELECT MAX(column_name) FROM table_name
20 MAX Retrieves the maximum value in a column.
WHERE condition;
SELECT MIN(column_name) FROM table_name
21 MIN Retrieves the minimum value in a column.
WHERE condition;
Groups rows that have the same values into SELECT column_name, COUNT(*) FROM table_name
22 GROUP BY
summary rows. GROUP BY column_name;
Specifies a search condition for a group or an SELECT column_name, COUNT(*) FROM table_name
23 HAVING
aggregate function. GROUP BY column_name HAVING COUNT(*) > 1;
SELECT column1, column2 FROM table_name ORDER
24 ORDER BY Sorts the result set in ascending or descending order.
BY column1 ASC, column2 DESC;
25 LIMIT Limits the number of rows returned in a result set. SELECT * FROM table_name LIMIT 10;
Specifies the starting point from which to return rows
26 OFFSET SELECT * FROM table_name OFFSET 5;
in a result set.
Retrieves rows from a result set after a specified SELECT * FROM table_name OFFSET 5 ROWS
27 FETCH
number of rows have been returned. FETCH NEXT 10 ROWS ONLY;
Combines the results of two or more SELECT SELECT column1 FROM table1 UNION SELECT
28 UNION
statements. column2 FROM table2;
Retrieves rows that appear in both result sets of two SELECT column1 FROM table1 INTERSECT SELECT
29 INTERSECT
SELECT statements. column2 FROM table2;
Retrieves rows that appear in the first result set but
SELECT column1 FROM table1 EXCEPT SELECT
30 EXCEPT not in the second result set of two SELECT
column2 FROM table2;
statements.
SELECT * FROM table_name WHERE column_name
31 LIKE Used to search for a specified pattern in a column.
LIKE 'pattern';

© www.atozlibrary.in Page:- 1
AtoZ Library Free Resources

All SQL Commands List with Example


Downloaded From atozibrary.in (Visit For More)
Sl No Command Description Syntax
Specifies multiple values for a column in a WHERE SELECT * FROM table_name WHERE column_name
32 IN
clause. IN (value1, value2, ...);
Retrieves rows where a value falls within a specified SELECT * FROM table_name WHERE column_name
33 BETWEEN
range. BETWEEN value1 AND value2;
Checks for the existence of rows in a subquery SELECT * FROM table_name WHERE EXISTS
34 EXISTS
result. (SELECT * FROM another_table WHERE condition);
35 NOT Negates a condition in a WHERE clause. SELECT * FROM table_name WHERE NOT condition;
SELECT * FROM table_name WHERE column_name >
36 ALL Compares a value to every value in a result set.
ALL (SELECT column_name FROM another_table);
SELECT * FROM table_name WHERE column_name >
37 ANY/SOME Compares a value to any value in a result set.
ANY (SELECT column_name FROM another_table);
Combines rows from two or more tables based on a SELECT * FROM table1 INNER JOIN table2 ON table1.
38 JOIN
related column between them. column_name = table2.column_name;
Retrieves all records from the left table and the SELECT * FROM table1 LEFT JOIN table2 ON table1.
39 LEFT JOIN
matched records from the right table. column_name = table2.column_name;
Retrieves all records from the right table and the SELECT * FROM table1 RIGHT JOIN table2 ON table1.
40 RIGHT JOIN
matched records from the left table. column_name = table2.column_name;
Retrieves all records when there is a match in either SELECT * FROM table1 FULL JOIN table2 ON table1.
41 FULL JOIN
the left or right table. column_name = table2.column_name;
42 CROSS JOIN Returns the Cartesian product of two tables. SELECT * FROM table1 CROSS JOIN table2;
Combines the results of two or more SELECT SELECT column1 FROM table1 UNION ALL SELECT
43 UNION ALL
statements, including duplicates. column2 FROM table2;
SELECT column_name, CASE WHEN condition THEN
44 CASE Adds conditional logic to a query.
value1 ELSE value2 END FROM table_name;
SELECT column_name FROM table_name WHERE
45 SUBQUERY A query nested within another query. column_name = (SELECT column_name FROM
another_table WHERE condition);
Opposite of EXISTS; checks for the absence of rows SELECT * FROM table_name WHERE NOT EXISTS
46 NOT EXISTS
in a subquery result. (SELECT * FROM another_table WHERE condition);
Creates an index on a table column for faster data CREATE INDEX index_name ON table_name
47 CREATE INDEX
retrieval. (column_name);
48 DROP INDEX Deletes an index from a table. DROP INDEX index_name;
Defines a column (or columns) that uniquely CREATE TABLE table_name (column1 datatype
49 PRIMARY KEY
identifies each record in a table. PRIMARY KEY, column2 datatype);
CREATE TABLE table_name (column1 datatype,
Establishes a link between two tables, ensuring
50 FOREIGN KEY column2 datatype, FOREIGN KEY (column2)
referential integrity.
REFERENCES another_table(column_name));
CREATE TABLE table_name (column1 datatype,
51 UNIQUE Ensures that all values in a column are distinct.
column2 datatype, UNIQUE (column1));
CREATE TABLE table_name (column1 datatype
52 CHECK Enforces constraints on column values.
CHECK (condition), column2 datatype);
ALTER TABLE table_name ADD CONSTRAINT
Specifies that changes to a parent table's rows will constraint_name FOREIGN KEY (column_name)
53 CASCADE
propagate to related child table rows. REFERENCES another_table(column_name) ON
DELETE CASCADE;
ALTER TABLE table_name ADD CONSTRAINT
Sets foreign key columns to NULL when a parent row constraint_name FOREIGN KEY (column_name)
54 SET NULL
is deleted. REFERENCES another_table(column_name) ON
DELETE SET NULL;
ALTER TABLE table_name ADD CONSTRAINT
Specifies that no action is taken when a parent row is constraint_name FOREIGN KEY (column_name)
55 NO ACTION
deleted. REFERENCES another_table(column_name) ON
DELETE NO ACTION;
ALTER TABLE table_name ADD CONSTRAINT
Prevents the deletion of a parent row if any child constraint_name FOREIGN KEY (column_name)
56 RESTRICT
rows exist. REFERENCES another_table(column_name) ON
DELETE RESTRICT;
CREATE TABLE table_name (column1 datatype
57 DEFAULT Specifies a default value for a column.
DEFAULT default_value, column2 datatype);

© www.atozlibrary.in Page:- 2
AtoZ Library Free Resources

All SQL Commands List with Example


Downloaded From atozibrary.in (Visit For More)
Sl No Command Description Syntax
Returns the first non-null value in a list of SELECT COALESCE(column_name1, column_name2,
58 COALESCE
expressions. 'default_value') FROM table_name;
Compares two expressions and returns NULL if they SELECT NULLIF(expression1, expression2) FROM
59 NULLIF
are equal; otherwise, returns the first expression. table_name;
SELECT column1, column2, SUM(column3) FROM
60 ROLLUP Generates subtotals for a specified group of columns.
table_name GROUP BY ROLLUP (column1, column2);
Generates subtotals and grand totals for all SELECT column1, column2, SUM(column3) FROM
61 CUBE
combinations of specified columns. table_name GROUP BY CUBE (column1, column2);
Assigns a unique sequential integer to each row in a SELECT ROW_NUMBER() OVER (ORDER BY
62 ROW_NUMBER
result set. column_name) AS row_num, * FROM table_name;
Assigns a unique integer to each distinct row in a SELECT DENSE_RANK() OVER (ORDER BY
63 DENSE_RANK
result set, with no gaps in the ranking. column_name) AS rank, * FROM table_name;
Divides a result set into a specified number of SELECT NTILE(4) OVER (ORDER BY column_name)
64 NTILE
roughly equal groups or "tiles." AS tile_num, * FROM table_name;
SELECT column_name, LEAD(column_name, 1) OVER
Accesses data from a subsequent row within the
65 LEAD (ORDER BY column_name) AS next_value FROM
same result set.
table_name;
SELECT column_name, LAG(column_name, 1) OVER
Accesses data from a previous row within the same
66 LAG (ORDER BY column_name) AS prev_value FROM
result set.
table_name;
Concatenates the values of string expressions and SELECT STRING_AGG(column_name, ', ') AS
67 STRING_AGG
separates them with a specified delimiter. concatenated_values FROM table_name;
Removes all records from a table quickly and
68 TRUNCATE TABLE TRUNCATE TABLE table_name;
efficiently, without logging individual row deletions.
69 SHOW TABLES Displays a list of tables in the current database. SHOW TABLES;
Provides information about the structure of a table,
70 DESCRIBE DESCRIBE table_name;
including column names, data types, and constraints.
71 CREATE DATABASE Creates a new database. CREATE DATABASE database_name;
72 DROP DATABASE Deletes an existing database. DROP DATABASE database_name;
Specifies the database to be used for subsequent
73 USE USE database_name;
queries.
Creates a virtual table based on the result of a CREATE VIEW view_name AS SELECT column1,
74 CREATE VIEW
SELECT query. column2 FROM table_name WHERE condition;
75 DROP VIEW Deletes an existing view. DROP VIEW view_name;
ALTER VIEW view_name AS SELECT new_column1,
76 ALTER VIEW Modifies an existing view definition. new_column2 FROM table_name WHERE
new_condition;
Creates an index on a table to improve query CREATE INDEX index_name ON table_name
77 INDEX
performance. (column_name);
Creates a unique index to enforce uniqueness on CREATE UNIQUE INDEX index_name ON table_name
78 UNIQUE INDEX
one or more columns. (column_name);
Provides information about how a SELECT statement EXPLAIN SELECT column1, column2 FROM
79 EXPLAIN
will be executed. table_name WHERE condition;
Analyzes and collects statistics about the distribution
80 ANALYZE ANALYZE table_name;
of data in tables.
Reclaims storage occupied by dead tuples and frees
81 VACUUM VACUUM table_name;
space for reuse.
CHECK Verifies that data in a table satisfies specified ALTER TABLE table_name ADD CONSTRAINT
82
CONSTRAINT conditions. constraint_name CHECK (condition);
UNIQUE Ensures that values in a column or a group of ALTER TABLE table_name ADD CONSTRAINT
83
CONSTRAINT columns are unique. constraint_name UNIQUE (column_name);
PRIMARY KEY Defines a column (or columns) as the primary key for ALTER TABLE table_name ADD CONSTRAINT
84
CONSTRAINT a table. constraint_name PRIMARY KEY (column_name);
ALTER TABLE table_name ADD CONSTRAINT
FOREIGN KEY
85 Establishes a relationship between two tables. constraint_name FOREIGN KEY (column_name)
CONSTRAINT
REFERENCES referenced_table (referenced_column);
ALTER TABLE table_name ADD CONSTRAINT
DEFAULT
86 Specifies a default value for a column. constraint_name DEFAULT default_value FOR
CONSTRAINT
column_name;

© www.atozlibrary.in Page:- 3
AtoZ Library Free Resources

All SQL Commands List with Example


Downloaded From atozibrary.in (Visit For More)
Sl No Command Description Syntax
NOT NULL ALTER TABLE table_name MODIFY column_name
87 Ensures that a column cannot contain NULL values.
CONSTRAINT datatype NOT NULL;
88 GRANT PRIVILEGES Gives users specific privileges on database objects. GRANT privilege ON object TO user;
REVOKE
89 Revokes previously granted privileges from users. REVOKE privilege ON object FROM user;
PRIVILEGES
90 CREATE USER Creates a new database user. CREATE USER username IDENTIFIED BY 'password';
91 DROP USER Deletes an existing database user. DROP USER username;
ALTER USER username IDENTIFIED BY
92 ALTER USER Modifies the properties of an existing database user.
'new_password';
93 SET PASSWORD Changes the password for a database user. SET PASSWORD FOR username = 'new_password';
Prevents other sessions from accessing or modifying
94 LOCK TABLE LOCK TABLE table_name IN EXCLUSIVE MODE;
a table.
95 UNLOCK TABLE Releases a lock on a table. UNLOCK TABLES;
BEGIN
96 Starts a new transaction. BEGIN TRANSACTION;
TRANSACTION
COMMIT Saves all changes made during the current
97 COMMIT;
TRANSACTION transaction.
ROLLBACK Undoes changes made during the current
98 ROLLBACK;
TRANSACTION transaction.
RELEASE
99 Removes a savepoint from the current transaction. RELEASE SAVEPOINT savepoint_name;
SAVEPOINT
DECLARE cursor_name CURSOR FOR SELECT
100 DECLARE CURSOR Declares a cursor to retrieve rows from a result set. column1, column2 FROM table_name WHERE
condition;
Opens a cursor to begin fetching rows from the result
101 OPEN CURSOR OPEN cursor_name;
set.
FETCH NEXT FROM cursor_name INTO @variable1,
102 FETCH CURSOR Retrieves rows from the result set through a cursor.
@variable2;
103 CLOSE CURSOR Closes a cursor and releases associated resources. CLOSE cursor_name;
DEALLOCATE
104 Removes a cursor definition from memory. DEALLOCATE cursor_name;
CURSOR
SET TRANSACTION SET TRANSACTION ISOLATION LEVEL READ
105 Sets the isolation level for a transaction.
ISOLATION LEVEL COMMITTED;
SHOW Displays information about active database
106 SHOW PROCESSLIST;
PROCESSLIST connections and their status.
107 KILL Terminates a database connection. KILL connection_id;
Displays the current values of MySQL server system
108 SHOW VARIABLES SHOW VARIABLES LIKE 'variable_name';
variables.
109 SHOW STATUS Retrieves server status variables. SHOW STATUS LIKE 'variable_name';
Modifies the value of a session variable or system
110 SET SET session_variable = value;
variable.
Resets the value of a session variable to its default
111 RESET RESET session_variable;
value.
Flushes various changes to disk and reloads
112 FLUSH FLUSH PRIVILEGES;
configuration files.
Analyzes and stores key distribution statistics for a
113 ANALYZE TABLE ANALYZE TABLE table_name;
table.
Checks the integrity of table data, indexes, and table
114 CHECK TABLE CHECK TABLE table_name;
structure.
115 REPAIR TABLE Repairs a corrupted table. REPAIR TABLE table_name;
Reorganizes table data to reduce storage space and
116 OPTIMIZE TABLE OPTIMIZE TABLE table_name;
improve performance.
BACKUP DATABASE database_name TO
117 BACKUP DATABASE Creates a backup of a database.
'backup_location';
RESTORE RESTORE DATABASE database_name FROM
118 Restores a database from a backup.
DATABASE 'backup_location';
119 SHOW GRANTS Displays the privileges granted to a user or role. SHOW GRANTS FOR user_name;

© www.atozlibrary.in Page:- 4
AtoZ Library Free Resources

All SQL Commands List with Example


Downloaded From atozibrary.in (Visit For More)
Sl No Command Description Syntax
SHOW CREATE
120 Generates the SQL statement used to create a table. SHOW CREATE TABLE table_name;
TABLE
121 SHOW INDEXES Displays information about indexes on a table. SHOW INDEXES FROM table_name;
SHOW TABLE
122 Retrieves information about tables in a database. SHOW TABLE STATUS;
STATUS
123 SHOW TRIGGERS Displays information about triggers in a database. SHOW TRIGGERS;
Retrieves information about scheduled events in a
124 SHOW EVENTS SHOW EVENTS;
database.
SHOW PROCEDURE Displays information about stored procedures in a
125 SHOW PROCEDURE STATUS;
STATUS database.
SHOW FUNCTION Retrieves information about stored functions in a
126 SHOW FUNCTION STATUS;
STATUS database.
SHOW CHARACTER Displays available character sets supported by the
127 SHOW CHARACTER SET;
SET server.
Retrieves information about collations supported by
128 SHOW COLLATION SHOW COLLATION;
the server.
129 SHOW DATABASES Lists all databases on the server. SHOW DATABASES;
Displays the last warning message generated by the
130 SHOW WARNINGS SHOW WARNINGS;
server.
Displays the last error message generated by the
131 SHOW ERRORS SHOW ERRORS;
server.
132 SHOW PRIVILEGES Retrieves available server privileges. SHOW PRIVILEGES;
SHOW FULL
133 Displays detailed information about active threads. SHOW FULL PROCESSLIST;
PROCESSLIST
SHOW GLOBAL
134 Displays global server status variables. SHOW GLOBAL STATUS;
STATUS
SHOW SLAVE
135 Retrieves information about replication slave status. SHOW SLAVE STATUS;
STATUS
SHOW MASTER Displays information about the binary log on the
136 SHOW MASTER STATUS;
STATUS master server.
SHOW VARIABLES
137 Filters server variables based on a pattern. SHOW VARIABLES LIKE 'pattern';
LIKE
SHOW BINARY
138 Lists binary log files on the server. SHOW BINARY LOGS;
LOGS
Displays information about storage engines
139 SHOW ENGINE SHOW ENGINE engine_name STATUS;
supported by the server.
140 SHOW ENGINES Retrieves a list of available storage engines. SHOW ENGINES;
SHOW FUNCTION
141 Displays the source code of a stored function. SHOW FUNCTION CODE function_name;
CODE
142 SHOW GRANTS FOR Retrieves the privileges granted to a user or role. SHOW GRANTS FOR user_name;
143 SHOW INDEX FROM Displays information about indexes on a table. SHOW INDEX FROM table_name;
SHOW INNODB Retrieves detailed information about the InnoDB
144 SHOW INNODB STATUS;
STATUS storage engine.
145 REPAIR Repairs a corrupted table. REPAIR TABLE table_name;
Reorganizes table data to reduce storage space and
146 OPTIMIZE OPTIMIZE TABLE table_name;
improve performance.
BACKUP DATABASE database_name TO
147 BACKUP Creates a backup of a database.
'backup_location';
RESTORE DATABASE database_name FROM
148 RESTORE Restores a database from a backup.
'backup_location';
Defines a trigger, which is a set of SQL statements
CREATE TRIGGER trigger_name BEFORE INSERT
149 CREATE TRIGGER that automatically "fires" when a specified event
ON table_name FOR EACH ROW BEGIN ... END;
occurs in a table.
150 DROP TRIGGER Deletes an existing trigger. DROP TRIGGER trigger_name;
151 DISABLE TRIGGER Disables a trigger, preventing it from firing. DISABLE TRIGGER trigger_name ON table_name;
152 ENABLE TRIGGER Enables a previously disabled trigger. ENABLE TRIGGER trigger_name ON table_name;
SHOW CREATE Generates the SQL statement used to create a
153 SHOW CREATE TRIGGER trigger_name;
TRIGGER trigger.

© www.atozlibrary.in Page:- 5
AtoZ Library Free Resources

All SQL Commands List with Example


Downloaded From atozibrary.in (Visit For More)
Sl No Command Description Syntax
154 SHOW INDEX Displays information about indexes on a table. SHOW INDEX FROM table_name;
SHOW MASTER
155 Lists the binary log files on the master server. SHOW MASTER LOGS;
LOGS
SHOW SLAVE Lists the slave servers connected to the master
156 SHOW SLAVE HOSTS;
HOSTS server.
SHOW STORAGE Displays information about storage engines
157 SHOW STORAGE ENGINES;
ENGINES supported by the server.

© www.atozlibrary.in Page:- 6

You might also like