Escolar Documentos
Profissional Documentos
Cultura Documentos
Manipulation
A instrução CREATE TABLE cria uma tabela numa base de dados. Permite
especificar o nome da tabela e o nome de cada coluna da tabela.
devem ser eliminados. Se a cláusula WHERE for omitida,
INSERT Statement (Instrução INSERT) todos os registos serão eliminados.
UPDATE Statement
table.
A instrução INSERT INTO é utilizada para adicionar um novo The UPDATE statement is used to edit records (rows) in a table. It
registo (linha) a uma tabela. includes a SET clause that indicates the column to edit and a
• Insert into columns in order. WHERE clause for specifying the record(s).
• Inserir nas colunas por ordem.
• Insert into columns by name. A instrução UPDATE é utilizada para editar registos (linhas)
• Inserir em colunas por nome. numa tabela. Inclui uma cláusula SET que indica a coluna a
editar e uma cláusula WHERE para especificar o(s) registo(s)
DELETE Statement
ADD column_name datatype;
AS Clause
SELECT name
FROM movies
WHERE name LIKE '_ove';
SELECT *
FROM contacts
ORDER BY Clause
ORDER BY birth_date DESC;
BETWEEN Operator
DISTINCT Clause
The BETWEEN operator can be used to
filter by a range of values. The range
Unique values of a column can be selected of values can be text, numbers, or
using a DISTINCT query. For a date data. The given query will match
table contact_details having five rows in any movie made between the years 1980
which the city column contains Chicago, and 1990, inclusive.
Madison, Boston, Madison, and Denver, the
O operador BETWEEN pode ser utilizado
given query would return:
para filtrar por um intervalo de
valores. O intervalo de valores pode ser
texto, números ou dados de data. A consulta
NULL Values
dada corresponderá a qualquer filme feito
entre os anos 1980 e 1990, inclusive.
Column values can be NULL, or have no
value. These records can be matched
SELECT * (or not matched) using the IS
FROM movies NULL and IS NOT NULL operators in
WHERE year BETWEEN 1980 AND 1990; combination with the WHERE clause. The
given query will match all addresses
where the address has a value or is
not NULL.
The WHERE clause is used to filter records The GROUP BY and ORDER BY clauses can
(rows) that match a certain condition. The reference the selected columns by
given query will select all records where number in which they appear in
the pub_year equals 2017. the SELECT statement. The example query
will count the number of movies per
A cláusula WHERE é utilizada para filtrar
rating, and will:
registos (linhas) que correspondem a uma
determinada condição. A consulta dada As cláusulas GROUP BY e ORDER BY podem
Aggregate Functions
rating
FROM movies
GROUP BY 2
ORDER BY 1;
SUM() Aggregate Function COUNT() Aggregate Function
The SUM() aggregate function takes the name The COUNT() aggregate function returns
of a column as an argument and returns the the total number of rows that match
sum of all the value in that column. the specified criteria. For instance,
to find the total number of employees
A função de agregação SUM() utiliza o nome
who have less than 5 years of
de uma coluna como argumento e devolve a
experience, the given query can be
soma de todos os valores dessa coluna.
used.
GROUP BY Clause
HAVING Clause
COUNT()
SELECT year,
SUM() ROUND(AVG(rating), 2)
FROM movies
MAX()
WHERE year = 2015;
MIN()
AVG()
ROUND() Function
diferentes tabelas, mesmo que a condição de Podem ser definidas várias tabelas
junção não seja satisfeita. Numa LEFT JOIN, temporárias com uma instância da
todas as linhas da tabela da esquerda são palavra-chave WITH.
devolvidas no conjunto de resultados e, se a
condição de união não for satisfeita, são
utilizados valores NULL para preencher as WITH temporary_movies AS (
UNION Clause
WITH Clause
The UNION clause is used to combine
The WITH clause stores the result of a query results that appear from
in a temporary table (temporary_movies) multiple SELECT statements and filter
using an alias. duplicates.
SELECT shirts.shirt_color,
pants.pants_color
FROM shirts
CROSS JOIN pants;
Foreign Key
SELECT *
FROM books
JOIN authors
ON books.author_id = authors.id;
Inner Join