This SQL tutorial consists of examples so you can quickly learn to use SQL commands, functions, querying data, conditional statements, set constraints, data types, views.
SQL (Structured Query Language) is used to manipulate data stored in relational database management systems (RDBMS). The SQL language is used in a database to create tables, views, insert data, query, update and delete.
Basic SQL tutorial
We begin the introduction to SQL tutorial by describing the chapters that will help you learn the basics of the SQL language, knowledge that will be useful for managing a relational database. The SQL tutorial chapters are the following:
SQL DDL Statements (Data Definition Language)
CREATE TABLE – learn how to create a table.
ALTER TABLE – show you how to modify a table structure.
DROP TABLE – remove a table from database.
TRUNCATE TABLE – allow to delete all rows from a table.
SQL DML Statements (Data Manipulation Language)
INSERT – insert rows into a table.
UPDATE – update values of existing rows in a table.
DELETE – delete rows from a table.
SQL Constraints
PRIMARY KEY – learn how to create table with a primary key.
FOREIGN KEY – define foreign key to link tables.
UNIQUE – show you how to define a unique constraint.
CHECK – show you how to create a check constraint for a table.
SQL Table Queries
SELECT – query a database table.
DISTINCT -is used to eliminate duplicate rows.
WHERE – is used to return the records that fulfill the conditions after WHERE clause.
AND – OR – show you syntax and examples.
BETWEEN – returns records between two values.
LIKE – return rows if the operand matches a pattern.
ORDER BY – is used to sort the rows in a query.
GROUP BY – is used to group values.
HAVING – show you how to filter groups.
INNER JOIN – returns rows from a query that match the specified join condition.
LEFT JOIN – returns all rows from the left table.
RIGHT JOIN – returns all rows from the right table.
FULL JOIN – returns all rows from the joined tables.
CROSS JOIN – returns the cartesian product of the tables.
SELF JOIN – show you how to use the same table in a self-join.
SQL Aggregate Functions
SQL Aggregate Functions allow us to perform calculations on groups of data. These functions operate on a set of values and return a single value that represents a summary of that set. There are several different types of aggregate functions that can be used in SQL:
AVG – returns the average of the value.
COUNT – show you how to count rows.
MAX – is used to get the maximum value.
MIN – is used to get the minimum value.
SUM – calculates the sum of values from a query.
Date Functions
Date functions are used to work with date and time values. They allow you to retrieve parts of a date, perform date arithmetic, and format or compare dates. Common use cases include calculating age, determining time intervals, extracting year or month values, and handling timestamps in reports and analytics.
GETDATE – returns the current date and time.
DATEADD – adds or subtracts a date interval.
DATEDIFF – calculates the difference between two dates.
DATEPART – extract parts of a date.
String Functions
String functions enable manipulation and transformation of text data. They are commonly used to format output, clean data, extract substrings, and combine or compare text values. These functions are especially useful when working with names, addresses, codes, and other character-based data.
LEN – returns the length of a string.
SUBSTRING – extracts part of a string.
CONCAT – combines multiple strings.
TRIM – removes leading and trailing spaces.
Ranking Functions
Ranking functions assign a rank or position to rows within a result set based on specified ordering criteria. They are typically used to determine top or bottom values, resolve ties, and perform comparisons within groups. Ranking functions are frequently applied in reporting, leaderboards, and performance analysis.
ROW_NUMBER – assigns a unique sequential number to each row within a result set, based on a specified order.
RANK – assigns a rank to each row within a result set, with gaps in ranking when there are ties.
DENSE_RANK – assigns a rank to each row within a result set, without gaps in ranking when there are ties.
Analytical Functions
Analytical functions perform calculations across sets of rows related to the current row. Unlike aggregate functions, they do not collapse rows, allowing detailed analysis while still showing individual records. These functions are essential for trend analysis, running totals, comparisons, and advanced reporting.
LAG – accesses data from a previous row.
LEAD – accesses data from a following row.
FIRST_VALUE – returns the first value of an ordered set of values.
LAST_VALUE – retrieves the last value in a set of ordered rows.
Math Functions
Math functions perform numerical calculations such as rounding, absolute values, exponentiation, and random number generation. They are commonly used in financial calculations, statistical analysis, and data transformations that require precise numerical operations.
ABS – returns absolute value.
ROUND – rounds a number.
CEILING – returns the smallest integer greater than or equal to a given numeric value.
POWER – returns the value of a number raised to the power of another number.