Essential Queries Guide
Learn how to perform essential SQL operations such as creating tables, inserting data, and using aggregate functions like MAX, MIN, and AVG.
The Essential Queries Guide offers a concise collection of commonly-used SQL queries. It's designed to help developers and database administrators quickly find syntax and examples for typical database operations, from table creation and data insertion to effective data retrieval and manipulation.
Creating a Table
To create new tables:
CREATE TABLE t1 ( a INT );CREATE TABLE t2 ( b INT );CREATE TABLE student_tests (
name CHAR(10), test CHAR(10),
score TINYINT, test_date DATE
);For more details, see the official CREATE TABLE documentation.
Inserting Records
To add data into your tables:
INSERT INTO t1 VALUES (1), (2), (3);INSERT INTO t2 VALUES (2), (4);INSERT INTO student_tests
(name, test, score, test_date) VALUES
('Chun', 'SQL', 75, '2012-11-05'),
('Chun', 'Tuning', 73, '2013-06-14'),
('Esben', 'SQL', 43, '2014-02-11'),
('Esben', 'Tuning', 31, '2014-02-09'),
('Kaolin', 'SQL', 56, '2014-01-01'),
('Kaolin', 'Tuning', 88, '2013-12-29'),
('Tatiana', 'SQL', 87, '2012-04-28'),
('Tatiana', 'Tuning', 83, '2013-09-30');For more information, see the official INSERT documentation.
Using AUTO_INCREMENT
The AUTO_INCREMENT attribute automatically generates a unique identity for new rows.
Create a table with an AUTO_INCREMENT column:
When inserting, omit the id field; it will be automatically generated:
Verify the inserted records:
For more details, see the AUTO_INCREMENT documentation.
Querying from two tables on a common value (JOIN)
To combine rows from two tables based on a related column:
This type of query is a join. For more details, consult the documentation on JOINS.
Finding the Maximum Value
To find the maximum value in a column:
See the MAX() function documentation. For a grouped example, refer to Finding the Maximum Value and Grouping the Results below.
Finding the Minimum Value
To find the minimum value in a column:
See the MIN() function documentation.
Finding the Average Value
To calculate the average value of a column:
See the AVG() function documentation.
Finding the Maximum Value and Grouping the Results
To find the maximum value within groups:
Further details are available in the MAX() function documentation.
Ordering Results
To sort your query results (e.g., in descending order):
For more options, see the ORDER BY documentation.
Finding the Row with the Minimum of a Particular Column
To find the entire row containing the minimum value of a specific column across all records:
Finding Rows with the Maximum Value of a Column by Group
To retrieve the full record for the maximum value within each group (e.g., highest score per student):
Calculating Age
Use the TIMESTAMPDIFF function to calculate age from a birth date.
To see the current date (optional, for reference):
To calculate age as of a specific date (e.g., '2014-08-02'):
To calculate current age, replace the specific date string (e.g., '2014-08-02') with CURDATE().
See the TIMESTAMPDIFF() documentation for more.
Using User-defined Variables
User-defined variables can store values for use in subsequent queries within the same session.
Example: Set a variable for the average score and use it to filter results.
Example: Add an incremental counter to a result set.
See User-defined Variables for more.
View Tables in Order of Size
To list all tables in the current database, ordered by their size (data + index) in megabytes:
Removing Duplicates
To remove duplicate rows based on specific column values, while keeping one instance (e.g., the instance with the highest id).
This example assumes id is a unique primary key and duplicates are identified by the values in column f1. It keeps the row with the maximum id for each distinct f1 value.
Setup sample table and data:
To delete duplicate rows, keeping the one with the highest id for each group of f1 values:
This query targets rows for deletion (t_del) where their f1 value matches an f1 in a subquery (t_keep) that has duplicates, and their id is less than the maximum id found for that f1 group.
Verify results after deletion:
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

