knowledge/technology/dev/programming/languages/SQL.md
2024-02-03 23:11:50 +01:00

29 KiB

obj
concept

SQL

Structured Query Language (SQL) is a powerful and standardized programming language used for managing and manipulating relational databases. It allows users to interact with databases to create, retrieve, update, and delete data, as well as define and modify the database schema. SQL is widely used in web development, data analysis, and various other domains where data storage and retrieval are essential.

A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.

SQL Statements

Most of the actions you need to perform on a database are done with SQL statements.
Example:

SELECT * FROM Customers;

Comments

Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.

Single Line Comments:

--Select all:
SELECT * FROM Customers;

Multi Line comments:

/*Select all the columns
of all the records
in the Customers table:*/
SELECT * FROM Customers;

SELECT

The SELECT statement is used to select data from a database.

Select:

SELECT column1, column2, ...  
FROM table_name;

Select all:

SELECT * FROM table

SELECT DISTINCT

The SELECT DISTINCT statement is used to return only distinct (different) values.

SELECT DISTINCT Country FROM Customers;
SELECT COUNT(DISTINCT Country) FROM Customers;

WHERE

The WHERE clause is used to filter records.

SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT * FROM Customers
WHERE Country='Mexico';
SELECT * FROM Customers
WHERE CustomerID=1;

The following operators can be used in the WHERE clause:

Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column

LIKE

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:
- The percent sign % represents zero, one, or multiple characters
- The underscore sign _ represents one, single character

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

The _ Wildcard

The _ wildcard represents a single character.
It can be any character or number, but each _ represents one, and only one, character.

SELECT * FROM Customers  
WHERE city LIKE 'L_nd__';

The % Wildcard

The % wildcard represents any number of characters, even zero characters.

SELECT * FROM Customers  
WHERE city LIKE '%L%';

Starts With

SELECT * FROM Customers  
WHERE CustomerName LIKE 'La%';

Ends With

SELECT * FROM Customers  
WHERE CustomerName LIKE '%a';

Contains

SELECT * FROM Customers  
WHERE CustomerName LIKE '%or%';

IN

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

SELECT * FROM Customers  
WHERE Country IN ('Germany', 'France', 'UK');

Subquery:

SELECT * FROM Customers  
WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);

BETWEEN

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

SELECT * FROM Orders  
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';

AND

The WHERE clause can contain one or many AND operators.

SELECT *
FROM Customers
WHERE Country = 'Spain' AND CustomerName LIKE 'G%';

OR

The WHERE clause can contain one or more OR operators.

SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';

Combine AND & OR

SELECT * FROM Customers
WHERE Country = 'Spain' AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');

NOT

The NOT operator is used in combination with other operators to give the opposite result, also called the negative result.

SELECT * FROM Customers  
WHERE NOT Country = 'Spain';

SELECT * FROM Customers  
WHERE City NOT IN ('Paris', 'London');

ORDER BY

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

INSERT INTO

The INSERT INTO statement is used to insert new records in a table.

Example:

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

Insert Many:

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');

UPDATE

The UPDATE statement is used to modify the existing records in a table.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE

The DELETE statement is used to delete existing records in a table.

Example:

DELETE FROM table_name WHERE condition;

Delete All:

DELETE FROM table_name;

Delete Table:

DROP TABLE table_name;

LIMIT

The LIMIT clause is used to specify the number of records to return.

SELECT * FROM Customers
LIMIT 3;

Aliases

SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of that query.
An alias is created with the AS keyword.

SELECT column_name AS alias_name
FROM table_name;

JOIN

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

INNER JOIN

The INNER JOIN keyword selects records that have matching values in both tables.

SELECT Orders.OrderID, Customers.CustomerName  
FROM Orders  
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Database

Create Database

The CREATE DATABASE statement is used to create a new SQL database.

CREATE DATABASE databasename;

Delete Database

The DROP DATABASE statement is used to drop an existing SQL database.

DROP DATABASE databasename;

Create Table

The CREATE TABLE statement is used to create a new table in a database.

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)
);

Delete Table

The DROP TABLE statement is used to drop an existing table in a database.

DROP TABLE table_name;

Change Table

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

Add Column:

ALTER TABLE Customers
ADD Email varchar(255);

Drop Column:

ALTER TABLE Customers  
DROP COLUMN Email;

Rename Column:

ALTER TABLE table_name
RENAME COLUMN old_name to new_name;

Change Datatype:

ALTER TABLE Persons
ALTER COLUMN DateOfBirth year;

Constraints

Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.

CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    column3 datatype constraint,
    ....
);

The following constraints are commonly used in SQL:

NOT NULL

By default, a column can hold NULL values.

The NOT NULL constraint enforces a column to NOT accept NULL values.

This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

CREATE TABLE Persons (  
    ID int NOT NULL,  
    LastName varchar(255) NOT NULL,  
    FirstName varchar(255) NOT NULL,  
    Age int  
);

UNIQUE

The UNIQUE constraint ensures that all values in a column are different.

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint.

However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

PRIMARY KEY

The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL values.

A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    PRIMARY KEY (ID)
);

FOREIGN KEY

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.

The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.

CREATE TABLE Orders (
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

CHECK

The CHECK constraint is used to limit the value range that can be placed in a column.

If you define a CHECK constraint on a column it will allow only certain values for this column.

If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

CREATE TABLE Persons (  
    ID int NOT NULL,  
    LastName varchar(255) NOT NULL,  
    FirstName varchar(255),  
    Age int,  
    CHECK (Age>=18)  
);

DEFAULT

The DEFAULT constraint is used to set a default value for a column.

The default value will be added to all new records, if no other value is specified.

CREATE TABLE Orders (  
    ID int NOT NULL,  
    OrderNumber int NOT NULL,  
    OrderDate date DEFAULT GETDATE()  
);

AUTO_INCREMENT

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table.

Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

CREATE TABLE Persons (  
    Personid int NOT NULL AUTO_INCREMENT,  
    LastName varchar(255) NOT NULL,  
    FirstName varchar(255),  
    Age int,  
    PRIMARY KEY (Personid)  
);

Create Index

The CREATE INDEX statement is used to create indexes in tables.

Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.

CREATE INDEX index_name
ON table_name (column1, column2, ...);

Dates

MySQL comes with the following data types for storing a date or a date/time value in the database:
- DATE - format YYYY-MM-DD
- DATETIME - format: YYYY-MM-DD HH:MI:SS
- TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
- YEAR - format YYYY or YY

Data Types

String

Data type Description
CHAR(size) A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the column length in characters - can be from 0 to 255. Default is 1
VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the maximum string length in characters - can be from 0 to 65535
BINARY(size) Equal to CHAR(), but stores binary byte strings. The size parameter specifies the column length in bytes. Default is 1
VARBINARY(size) Equal to VARCHAR(), but stores binary byte strings. The size parameter specifies the maximum column length in bytes.
TINYBLOB For BLOBs (Binary Large Objects). Max length: 255 bytes
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT(size) Holds a string with a maximum length of 65,535 bytes
BLOB(size) For BLOBs (Binary Large Objects). Holds up to 65,535 bytes of data
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters
MEDIUMBLOB For BLOBs (Binary Large Objects). Holds up to 16,777,215 bytes of data
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
LONGBLOB For BLOBs (Binary Large Objects). Holds up to 4,294,967,295 bytes of data
ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are sorted in the order you enter them
SET(val1, val2, val3, ...) A string object that can have 0 or more values, chosen from a list of possible values. You can list up to 64 values in a SET list

Numeric

Data type Description
BIT(size) A bit-value type. The number of bits per value is specified in size. The size parameter can hold a value from 1 to 64. The default value for size is 1.
TINYINT(size) A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to 255. The size parameter specifies the maximum display width (which is 255)
BOOL Zero is considered as false, nonzero values are considered as true.
BOOLEAN Equal to BOOL
SMALLINT(size) A small integer. Signed range is from -32768 to 32767. Unsigned range is from 0 to 65535. The size parameter specifies the maximum display width (which is 255)
MEDIUMINT(size) A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from 0 to 16777215. The size parameter specifies the maximum display width (which is 255)
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range is from 0 to 4294967295. The size parameter specifies the maximum display width (which is 255)
INTEGER(size) Equal to INT(size)
BIGINT(size) A large integer. Signed range is from -9223372036854775808 to 9223372036854775807. Unsigned range is from 0 to 18446744073709551615. The size parameter specifies the maximum display width (which is 255)
FLOAT(size, d) A floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. This syntax is deprecated in MySQL 8.0.17, and it will be removed in future MySQL versions
FLOAT(p) A floating point number. MySQL uses the p value to determine whether to use FLOAT or DOUBLE for the resulting data type. If p is from 0 to 24, the data type becomes FLOAT(). If p is from 25 to 53, the data type becomes DOUBLE()
DOUBLE(size, d) A normal-size floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter
DOUBLE PRECISION(size, d)
DECIMAL(size, d) An exact fixed-point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. The maximum number for size is 65. The maximum number for d is 30. The default value for size is 10. The default value for d is 0.
DEC(size, d) Equal to DECIMAL(size,d)

Date & Time

Data type Description
DATE A date. Format: YYYY-MM-DD. The supported range is from '1000-01-01' to '9999-12-31'
DATETIME(fsp) A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The supported range is from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. Adding DEFAULT and ON UPDATE in the column definition to get automatic initialization and updating to the current date and time
TIMESTAMP(fsp) A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch ('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD hh:mm:ss. The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC. Automatic initialization and updating to the current date and time can be specified using DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP in the column definition
TIME(fsp) A time. Format: hh:mm:ss. The supported range is from '-838:59:59' to '838:59:59'
YEAR A year in four-digit format. Values allowed in four-digit format: 1901 to 2155, and 0000.
MySQL 8.0 does not support year in two-digit format.

Functions

String Functions

CHARACTER_LENGTH()

The CHARACTER_LENGTH() function return the length of a string (in characters).

SELECT CHARACTER_LENGTH("SQL Tutorial") AS LengthOfString;

CONCAT()

The CONCAT() function returns a concat string of parameters.

SELECT CONCAT(Name, LastName)
FROM persons;

LOWER() & UPPER()

The UPPER() function converts a string to upper-case.
The LOWER() function converts a string to lower-case.

SELECT LOWER("SQL");
SELECT UPPER("SQL");

TRIM() (RTRIM() & LTRIM())

The TRIM() function removes leading and trailing spaces from a string. LTRIM() & RTRIM() remove leading and trailing spaces from the left or right respectively.

SELECT TRIM('    SQL    ') AS TrimmedString;

SUBSTRING()

The SUBSTRING() function extracts a substring from a string (starting at any position).

SUBSTRING(string, start, length)

LPAD() & RPAD()

The LPAD() & RPAD() function left- or right-pads a string with another string, to a certain length.

RPAD(string, length, rpad_string)

LEFT() & RIGHT()

These functions extract a number of characters from a string. (From left or right respectively)

RIGHT(string, number_of_chars)

REVERSE()

The REVERSE() function reverses a string and returns the result.

REPLACE()

The REPLACE() function replaces all occurrences of a substring within a string, with a new substring.

REPLACE(string, from_string, new_string)

REPEAT()

The REPEAT() function repeats a string as many times as specified.

REPEAT(string, number)

Numeric Functions

MIN() & MAX()

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

SELECT MIN(Price)  
FROM Products;

SELECT MAX(Price)  
FROM Products;

COUNT()

The COUNT() function returns the number of rows that matches a specified criterion.

SELECT COUNT(*)  
FROM Products;

SUM()

The SUM() function returns the total sum of a numeric column.

Example:

SELECT SUM(Quantity) AS total  
FROM OrderDetails;

Expressions:

SELECT SUM(Quantity * 10)  
FROM OrderDetails;

AVG()

The AVG() function returns the average value of a numeric column.

SELECT AVG(Price)  
FROM Products;

SELECT * FROM Products  
WHERE price > (SELECT AVG(price) FROM Products);

Date Functions

Extract information

  1. DAY(): Returns the day of the month for a given date.
  2. HOUR(): Returns the hour part for a given date.
  3. MINUTE(): Returns the minute part of a time/datetime.
  4. SECOND(): Returns the seconds part of a time/datetime.
  5. MICROSECOND(): Returns the microsecond part of a time/datetime.
  6. DAYNAME(): Returns the weekday name for a given date.
  7. MONTH(): Returns the month for a given date
  8. MONTHNAME(): Returns the name of the month for a given date.
  9. DAYOFMONTH(): Returns the day of the month for a given date.
  10. DAYOFWEEK(): Returns the weekday index for a given date.
  11. DAYOFYEAR(): Returns the day of the year for a given date.
  12. QUARTER(): Returns the quarter of the year for a given date value.
  13. WEEKDAY(): Returns the weekday number for a given date.
  14. WEEKOFYEAR(): Returns the week number for a given date.

Create Datetimes

Current date and time:

NOW()