vadnica-logo
Editor My Blog My Services About the Project Telegram O meni O meni

MySQL SQL Commands and Statements

Discover the most important MySQL SQL commands and statements. On this page, you will find an overview of all key SQL commands, their syntax, and practical usage examples. Learn how to correctly use SELECT, INSERT, UPDATE, DELETE, and other important SQL statements. The most common SQL commands:

  • baza.sql
💡 The SQL simulator you see here is my own work, written in JavaScript — not a real database. I try to cover as many cases as possible, but if it ever behaves strangely, don't trust it blindly — better check the example locally. 🙂
EXAMPLE
RESULT

The first example shows all data about orders over 1000 €

SELECT order_id, date, product, total_price 
FROM orders 
WHERE total_price > 1000 
ORDER BY total_price DESC;

Example of the INSERT command for adding a new customer to the table

INSERT INTO customers (id, first_name, last_name, email, phone, city) 
VALUES (16, 'Janez', 'Kranjski', 'janez.kranjski@email.com', '000-123-456', 'Ljubljana');

Display of all data from the customer’s table after adding a new customer

SELECT * FROM customers;

Display of data for employee Matej Kovačič before the update

SELECT * FROM employees WHERE id = 1;

Updating the employee salary with the UPDATE command

UPDATE employees 
SET salary = 2200.50 
WHERE first_name = 'Matej' AND last_name = 'Kovačič';

Display of data for employee Matej Kovačič after the update

SELECT * FROM employees WHERE id = 1;

MySQL Commands Quick Cheat Sheet

To help you find the right solution faster, we have prepared a comprehensive overview of key MySQL statements, commands, and functions. The table provides a short description of their purpose, the correct syntax, and a practical example that you can directly test inside our online SQL simulator.

Name Description Syntax (HTML) Example
SELECT Selects and retrieves data from one or more tables in a database. SELECT column1, column2 FROM table_name; SELECT first_name, last_name FROM employees;
INSERT INTO Inserts new rows of data into a specified table. INSERT INTO table_name (col1, col2) VALUES (val1, val2); INSERT INTO students (first_name, last_name) VALUES ('John', 'Doe');
UPDATE Modifies and updates existing data within a table. UPDATE table_name SET column = value WHERE condition; UPDATE employees SET salary = 2500.00 WHERE id = 1;
DELETE Deletes specific records (rows) from a table. DELETE FROM table_name WHERE condition; DELETE FROM customers WHERE id = 5;
CREATE DATABASE Creates a new, empty database. CREATE DATABASE database_name; CREATE DATABASE tutorial_db;
CREATE TABLE Creates a new table with defined columns and data types. CREATE TABLE table_name (column type, ...); CREATE TABLE test_table (id INT, name VARCHAR(50));
ALTER TABLE Modifies the structure of an existing table (adding, dropping columns). ALTER TABLE table_name ADD column_name type; ALTER TABLE employees ADD phone VARCHAR(20);
DROP DATABASE Permanently deletes an entire database along with all its tables and data. DROP DATABASE database_name; DROP DATABASE old_database;
DROP TABLE Permanently deletes a table, its structure, and all its records. DROP TABLE table_name; DROP TABLE first_table;
TRUNCATE TABLE Quickly empties all content from a table while keeping its structure intact. TRUNCATE TABLE table_name; TRUNCATE TABLE change_log;
PRIMARY KEY Defines a primary key to uniquely identify each row in a table. id INT PRIMARY KEY AUTO_INCREMENT CREATE TABLE users (id INT PRIMARY KEY);
FOREIGN KEY A foreign key that links a table to the primary key of another table. FOREIGN KEY (column) REFERENCES other_table(id) FOREIGN KEY (student_id) REFERENCES students(student_id)
INDEX Creates an index on specific columns for faster data retrieval. CREATE INDEX index_name ON table_name (column); CREATE INDEX idx_last_name ON employees (last_name);
UNIQUE Ensures that all values in a specific column are distinct. column_name type UNIQUE email VARCHAR(100) UNIQUE
WHERE Filters query results to extract only records that match a specific condition. SELECT * FROM table_name WHERE condition; SELECT * FROM sorting WHERE department = 'Sales';
GROUP BY Groups rows that have the same values into summary rows for aggregation. SELECT col, COUNT(*) FROM table_name GROUP BY col; SELECT department, AVG(salary) FROM employees GROUP BY department;
HAVING Filters groups of data that have already been grouped (used with GROUP BY). ... GROUP BY column HAVING aggregate_condition; SELECT department FROM employees GROUP BY department HAVING AVG(salary) > 2000;
ORDER BY Sorts the displayed results by one or more columns. SELECT * FROM table_name ORDER BY column DESC; SELECT * FROM sorting ORDER BY salary DESC;
JOIN Combines rows from multiple tables based on a related column between them. SELECT * FROM t1 JOIN t2 ON t1.id = t2.t1_id; SELECT s.first_name, o.grade FROM students s JOIN student_grades o ON s.student_id = o.student_id;
UNION Combines the results of two or more SELECT queries (excluding duplicates). SELECT col FROM t1 UNION SELECT col FROM t2; SELECT first_name FROM students UNION SELECT first_name FROM employees;
INTERSECT Returns only the records that appear in both SELECT queries. SELECT col FROM t1 INTERSECT SELECT col FROM t2; SELECT first_name FROM customers INTERSECT SELECT first_name FROM employees;
SUBQUERY A nested query inside a larger, main SQL query. SELECT * FROM t1 WHERE col = (SELECT max FROM t2); SELECT * FROM products WHERE price > (SELECT AVG(price) FROM products);
GRANT Gives a user specific security privileges and access rights. GRANT privilege ON db.* TO 'user'@'localhost'; GRANT SELECT, INSERT ON tutorial_db.* TO 'guest'@'localhost';
REVOKE Removes previously granted privileges from a user. REVOKE privilege ON db.* FROM 'user'@'localhost'; REVOKE DELETE ON tutorial_db.* FROM 'guest'@'localhost';
START TRANSACTION Begins a transaction (a group of operations executed as a single unit). START TRANSACTION; START TRANSACTION; UPDATE invoices SET amount = 0 WHERE id = 1;
COMMIT Confirms and permanently saves all changes made during the transaction. COMMIT; COMMIT;
ROLLBACK Cancels transaction changes, reverting the data to its previous state. ROLLBACK; ROLLBACK;
COUNT() Counts and returns the number of rows that match a condition. SELECT COUNT(column) FROM table_name; SELECT COUNT(*) FROM employees WHERE department = 'Production';
SUM() Calculates and returns the total sum of a numeric column. SELECT SUM(column) FROM table_name; SELECT SUM(total_price) FROM orders;
AVG() Calculates and returns the average value of a numeric column. SELECT AVG(column) FROM table_name; SELECT AVG(salary) FROM sorting;
MIN() Finds and returns the lowest (minimum) value in a column. SELECT MIN(column) FROM table_name; SELECT MIN(price) FROM products;
MAX() Finds and returns the highest (maximum) value in a column. SELECT MAX(column) FROM table_name; SELECT MAX(grade) FROM grades;