Common MySQL Queries (original) (raw)

Last Updated : 23 Mar, 2026

MySQL Server is an open-source relational database management system widely used for web applications to store and manage data efficiently. It powers many modern websites by enabling reliable data storage and seamless data exchange.

Queries

Queries are commands used to interact with database tables and manipulate data. Some commonly used MySQL queries, operators, and functions are listed below:

1. SHOW DATABASES

Displays all the databases available on the MySQL server, allowing users to view and verify existing databases before performing any operations.

**Syntax:

SHOW DATABASES;

2. USE database_name

Sets a specific database as the active database so that all subsequent queries are executed within that database.

**Syntax:

USE database_name;
SELECT DATABASE();

3. DESCRIBE table_name

Provides detailed information about the structure of a table, including column names, data types, and constraints.

**Syntax:

DESCRIBE table_name;

4. SHOW TABLES

Lists all the tables present in the currently selected database, helping users understand the database structure.

**Syntax:

SHOW TABLES;

5. SHOW CREATE TABLE table_name

Displays the complete SQL statement used to create a specific table, including all constraints and settings.

**Syntax:

SHOW CREATE TABLE table_name;

6. SELECT NOW()

Returns the current date and time from the MySQL server, which is useful for time-based operations.

**Query:

SELECT NOW();

**Output:

Screenshot-2026-03-23-110028

7. SELECT (Expressions)

Allows execution of calculations or evaluation of expressions without referencing any table.

**Query:

SELECT 2 + 4;
SELECT 2 + 4, CURDATE();

**Output :

Screenshot-2026-03-23-110254

Used to include notes or explanations inside SQL queries, improving readability and maintainability of code.

**Example:

/* Multi-line comment */

Single-line comment

-- Single-line comment

9. CREATE DATABASE

Creates a new database where tables and data can be stored and managed.

**Syntax:

CREATE DATABASE database_name;

10. DROP DATABASE

Deletes an existing database along with all its tables and data permanently.

**Syntax:

DROP DATABASE database_name;

11. CREATE TABLE

Creates a new table in the database with specified columns, data types, and constraints. It defines how data will be structured and stored in the database.

**Syntax:

CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
....,
columnN datatype constraints
);

**Query:

CREATE TABLE employee(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
profile VARCHAR(40) DEFAULT 'engineer'
) ENGINE = InnoDB;

12. AUTO_INCREMENT

Used to automatically generate unique sequential values for a column, typically for primary keys in a table.

**Syntax:

column_name INT AUTO_INCREMENT

13. DROP TABLE

Deletes an existing table from the database along with all its data and structure permanently.

**Syntax:

DROP TABLE table_name;

14. RENAME TABLE

Renames an existing table to a new name without affecting its data or structure.

**Syntax:

RENAME TABLE old_table_name TO new_table_name;

15. ALTER TABLE (ADD COLUMN)

Adds one or more new columns to an existing table, allowing modification of table structure after creation.

**Syntax:

ALTER TABLE table_name ADD column_name datatype;

16. ALTER TABLE (DROP COLUMN)

Removes a column from an existing table along with all its stored data.

**Syntax:

ALTER TABLE table_name DROP COLUMN column_name;

17. INSERT INTO

Inserts new records into a table by specifying column names and corresponding values. It is one of the most commonly used queries to add data into a database.

**Syntax:

INSERT INTO table_name (column1, column2)
VALUES (value1, value2);

**Query:

INSERT INTO employee (name, profile)
VALUES ('John', 'Developer');

**Output:

Screenshot-2026-03-23-113713

18. UPDATE

Updates existing records in a table based on specified conditions, allowing modification of stored data without deleting it.

**Syntax:

UPDATE table_name
SET column1 = value1
WHERE condition;

**Query:

UPDATE employee
SET profile = 'Manager'
WHERE id = 1;

**Output:

Screenshot-2026-03-23-114537

19. DELETE

Deletes records from a table based on a given condition. It is used to remove unwanted or outdated data from the database.

**Syntax:

DELETE FROM table_name WHERE condition;

20. SELECT with WHERE

Retrieves specific records from a table that satisfy a given condition, making it possible to filter data effectively.

**Syntax:

SELECT column1 FROM table_name WHERE condition;

**Query:

SELECT name FROM employee WHERE profile = 'Manager';

**Output:

Screenshot-2026-03-23-115443

21. SELECT *

Fetches all columns and all records from a table without specifying individual column names.

**Syntax:

SELECT * FROM table_name;

**Query:

SELECT * FROM Student;

**Output:

Screenshot-2026-03-23-120612

Student Table

22. COUNT()

Returns the total number of rows in a table or the number of rows that match a specific condition.

**Syntax:

SELECT COUNT(*) FROM table_name;

**Query:

SELECT COUNT(*) FROM Student;

**Output:

Screenshot-2026-03-23-120827

23. MAX()

Returns the maximum (highest) value from a specified column, useful for identifying top values in a dataset.

**Syntax:

SELECT MAX(column_name) FROM table_name;

**Query:

SELECT MAX(marks) FROM Student;

**Output:

Screenshot-2026-03-23-120939

24. MIN()

Returns the minimum (lowest) value from a specified column, helping identify the smallest value in a dataset.

**Syntax:

SELECT MIN(column_name) FROM table_name;

**Query:

SELECT MIN(marks) FROM Student;
-- Nested Query
SELECT MIN(marks)
FROM Student
WHERE marks > ( SELECT MIN(marks) from Student);

**Output:

Screenshot-2026-03-23-121744

25. LIMIT

Restricts the number of rows returned in a query result, helping control output size and improve performance.

**Syntax:

SELECT * FROM table_name LIMIT number;

**Query:

SELECT * FROM Student LIMIT 2;

**Output:

Screenshot-2026-03-23-122129

26. BETWEEN

Filters records where a column value falls within a specified range, including both lower and upper limits.

**Syntax:

SELECT * FROM table_name WHERE column BETWEEN value1 AND value2;

**Query:

SELECT * FROM Student WHERE grade BETWEEN 4 AND 6;

**Output:

Screenshot-2026-03-23-123035

27. DISTINCT

Returns only unique values from a column by removing duplicate entries from the result set.

**Syntax:

SELECT DISTINCT column_name FROM table_name;

**Query:

SELECT DISTINCT city FROM Student;

**Output:

Screenshot-2026-03-23-123630

28. IN

Checks whether a column value matches any value from a specified list, simplifying multiple OR conditions.

**Syntax:

SELECT * FROM table_name WHERE column IN (value1, value2);

**Query:

SELECT * FROM Student WHERE grade IN (6, 7, 8, 9);

Screenshot-2026-03-23-124051

29. AND

Combines multiple conditions where all conditions must be true for a record to be included in the result.

**Syntax:

SELECT * FROM table_name WHERE condition1 AND condition2;

**Query:

SELECT * FROM Student WHERE marks > 90 AND grade = 7;

**Output:

Screenshot-2026-03-23-125004

30. OR

Returns records if at least one of the specified conditions is true, allowing broader filtering.

**Syntax:

SELECT * FROM table_name WHERE condition1 OR condition2;

**Query:

SELECT * FROM Student WHERE city = 'London' OR city = 'Berlin';

**Output:

Screenshot-2026-03-23-125412

31. IS NULL

Checks whether a column contains NULL (missing or undefined) values in the table.

**Query:

SELECT * FROM Student WHERE city IS NULL;

32. FOREIGN KEY

Defines a relationship between two tables by linking a column to the primary key of another table.

**Syntax:

FOREIGN KEY (column_name) REFERENCES parent_table(column_name);

33. LIKE

Searches for records that match a specified pattern in a column, commonly used with text data.

**Syntax:

SELECT * FROM table_name WHERE column LIKE pattern;

**Query:

-- Matches names with "m" as the second character
SELECT * FROM Student WHERE name LIKE '_m%';

-- Matches names containing "ia" anywhere in the string
SELECT * FROM Student WHERE name LIKE '%ia%';

**Output:

Screenshot-2026-03-23-141505

34. JOINS

Joins are used to combine data from two or more tables based on a common column, allowing retrieval of related information stored across multiple tables.

**Syntax:

SELECT columns
FROM table1
JOIN table2 ON condition;

**INNER JOIN (Regular Join):

Returns only the records that have matching values in both tables based on the given condition.

SELECT student.name, department.name
FROM student
JOIN department ON student.department = department.name;

**LEFT JOIN:

Returns all records from the left table and the matching records from the right table. If no match is found, NULL values are returned for the right table.

SELECT student.name, department.name
FROM student
LEFT JOIN department ON student.department = department.name;

**RIGHT JOIN:

Returns all records from the right table and the matching records from the left table. If no match is found, NULL values are returned for the left table.

SELECT student.name, department.name
FROM student
RIGHT JOIN department ON student.department = department.name;