SQL Simplified: A Guide for Beginners, Developers, and Data Analysts
For SQL beginners seeking a solid foundation, seasoned developers aiming to fill knowledge gaps in table creation and data retrieval, and data analysts looking to refine their querying skills for actionable insights.
![]() |
| Simplified SQL Quick Guide For Quick wins |
SQL Table Creation
The following SQL statement creates a table named example with specific constraints and relationships.
create table example (
id int generated always as identity primary key,
name varchar(50) not null,
user_id int,
price decimal(10,2) check(price >= 0),
unique(name, id),
constraint fk_user_id foreign key(user_id)
references users(id)
on delete set null on update cascade
)Breakdown of Concepts in the Statement
Concept | Description | Example |
|---|---|---|
| Automatically generates a unique integer for each row, serving as the primary key (uniquely identifies each row). |
|
| Defines a column |
|
| Defines a column |
|
| Defines a column |
|
| Ensures the combination of | No two rows can have the same |
| Creates a foreign key constraint: | If |
| If a referenced user in | If |
| If a referenced user in | If |
DRL (Data Retrieval Language) Concepts
Basic Queries
select username as "Noms", 10 + 2 as resultat from usersasis optional and used for aliasing (renaming columns in the result).
Example:select u.username from users uRenames
usernametoNomsand calculates10 + 2asresultat.select distinct country from users
Returns unique values ofcountry(no duplicates).concat
Concatenates strings or field values.
Syntax:concat(string1, string2)orstring1 || string2.
Example:select concat(first_name, ' ', last_name) as full_name from users;
Filtering with WHERE
wherefilters rows based on conditions.Operators:
=,>,<,>=,<=,<>,between,in,like,is null,is not null.Example:
select * from users where age > 18 and country = 'Belgium';
LIKE vs ILIKE
Operator | Description | Example | Result |
|---|---|---|---|
| Case sensitive pattern matching. |
| Matches "John", "Jane" (not "john"). |
| Case insensitive pattern matching (PostgreSQL specific). |
| Matches "John", "jane", "JOHN". |
| Wildcard: Matches any sequence of characters (including none). |
| "John", "Jenny", "J". |
| Wildcard: Matches exactly one character. |
| "Jane", "June" (not "Jenny"). |
Sorting with ORDER BY
Sorts results by one or more columns.
Example:
select * from users order by last_name ASC, age DESC;Sorts by last_name (A Z) and then by age (highest first).
Handling NULL Values
Operator | Description | Example |
|---|---|---|
| Checks if a value is NULL. |
|
| Checks if a value is not NULL. |
|
| Incorrect! NULL is not equal to anything, even itself. Use | Not valid |
Logical Operators
Operator | Description | Example |
|---|---|---|
| Negates a condition. |
|
| Alternative to NOT (in some SQL dialects). |
|
| Checks if a string matches a regular expression (PostgreSQL specific). |
|
Operator Precedence in SQL
SQL evaluates operators in this order (highest to lowest):
Parentheses ()
NOT
AND
OR
Comparison operators (=, >, <, etc.)
Example:
where age > 18 AND (country = 'Belgium' OR country = 'France')AND is evaluated before OR unless parentheses are used.
Quotes vs. Single Quotes
Usage | Example | Purpose |
|---|---|---|
Double Quotes |
| For aliases, table names, or column names (e.g. with spaces). |
Single Quotes |
| For string literals (actual values). |
SQL Functions
Native SQL Functions
Category | Function | Description | Example |
|---|---|---|---|
Date/Time |
| Returns the current date. |
|
| Returns the current time. |
| |
| Returns the current date and time. |
| |
| Extracts a part of a date (e.g. year, month). |
| |
| Converts a date to a formatted string. |
| |
String |
| Returns the position of a substring (case sensitive, starts at 1). |
|
| Returns the length of a string. |
| |
| Extracts a substring from a string. |
| |
| Returns the left/right part of a string. |
| |
| Converts a string to uppercase/lowercase. |
| |
| Replaces a substring with another. |
| |
| Removes leading/trailing spaces. |
| |
Numeric |
| Returns the absolute value. |
|
| Returns the remainder of a division. |
| |
Type Casting |
| Converts a value to a different type. |
|
Aggregation Functions
Function | Description | Example |
|---|---|---|
| Counts the number of rows (returns bigint). |
|
| Returns the maximum value in a column. |
|
| Returns the minimum value in a column. |
|
| Returns the average value in a column. |
|
| Returns the sum of values in a column. |
|
| Conditional logic (like if else). |
|
| Returns NULL if two values are equal. |
|
| Returns the first non NULL value in a list. |
|
Summary
This article covers:
- SQL table creation with constraints.
- Data retrieval using SELECT, WHERE, ORDER BY, and logical operators.
- Handling NULL values and pattern matching with LIKE/ILIKE.
- SQL functions for strings, numbers, dates, and aggregations.

Comments
Post a Comment