DML – Data Manipulation Language
Create table:
CREATE TABLE nameoftable(id INTEGER PRIMARY KEY, name TEXT);
Add a row:
INSERT INTO nameoftable (nameOfcolumn1,2,3) VALUES (new1,new2,new3);View the row you just created:
SELECT * FROM nameoftable;Edit information in the table (for only existing records):
Add new column in the table:
Delete rows with no value:
Return uniq values:
Filter the result in rows only if condition is true:
Operators:
Filter the result to compare similar values:
Combine operator AND:
OR operator:
Sort the result:
Limiting the results:
Functions
Calculate the number of rows:
To combine the rows
CONCAT ( || ‘somethting’ ||):
Round to a whole number:
SELECT ROUND(345.156, 2);
If null return your value:
SUM of all the values in that column:
Find the largest value in a column:
Find the minimum value in a column:
Find average value:
GROUP BY
is using with COUNT, MIN, MAX, AVG, SUM
Multiple tables join
- left JOIN — every row in the lefttable is returned in the result set
- right JOIN — every in the right
- inner join=join
Union
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
The UNION ALL clause allows us to utilize information from multiple tables in our queries, including duplicate values.
INTERSECT is used to combine two SELECT statements, but returns rows only from the first SELECT statement that are identical to a row in the second SELECT statement.
EXCEPT returns distinct rows from the first SELECT statement that aren’t output by the second SELECT statement
Dates
DATEDIFF
DATE_ADD(date, interval 45, DAY)
SELECT name_city, name_author, DATE_ADD('2020-01-01', INTERVAL (RAND() * 365) DAY) AS Date FROM city, author ORDER BY 1, Date DESC
| Function | how to use | example |
| count(x) | ||
| sum | ||
| max | ||
| CEILING(x) | CEILING(4.2)=5 CEILING(-5.8)=-5 | |
| ROUND(x, k) | ROUND(4.361)=4 ROUND(5.86592,1)=5.9 | |
| FLOOR(x) | FLOOR(4.2)=4 FLOOR(-5.8)=-6 | |
| POWER(x, y) | POWER(3,4)=81.0 | |
| SQRT(x) | SQRT(4)=2.0 SQRT(2)=1.41… | |
| ABS(x) | ABS(-1) = 1 ABS(1) = 1 | |
| RAND() | random | |


