Learning PostgreSQL (SQL language)

Learning PostgreSQL (SQL language)

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

The source of the rows for the query is specified after the ___ keyword.

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

1

All-time users

1

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (109)

Section 1

(50 cards)

The source of the rows for the query is specified after the ___ keyword.

Front

FROM

Back

Simply speaking, DML has only four types of statements:

Front

- INSERT is used to put new data into the database - UPDATE is used to change the data - DELETE is used to delete the data - SELECT is used to retrieve the data

Back

The source of the rows for the query can be any of:

Front

- Table - View - Function - Subquery - Values

Back

Data Definition Language (DDL)

Front

used to create and manage the structure of the data

Back

SQL expressions can contain:

Front

- Column names (most of the cases) - Constants - Operator invocations - Parenthesis to control operations precedence - Function calls - Aggregate expressions (we will discuss them later) - Scalar subqueries - Type casts - Conditional expressions

Back

If the name of the schema is included in the ___ setting or if the object belongs to the current user's schema, then it is not required to use the schema name when referring to the object.

Front

search_path

Back

There is ___ of the SELECT statement that is always mandatory.

Front

no part

Back

ALL returns ___ the rows, this is the default.

Front

all

Back

The FROM clause is ___ in PostgreSQL but in other RDBMS such as Oracle the FROM keyword may be ___.

Front

optional, required

Back

Structured Query Language (SQL) is used to

Front

- set up the structure of the database - set up the structure of the database - query the database

Back

Comments are quoted in pairs of ___ . Also, the whole line of code can be commented by using ___.

Front

/ and /, double dash ( -- )

Back

Inner join can be shown as a filled area on the diagram, like this:

Front

Back

It is possible to nest commands, using a result of one command as an input for another command. That nested queries are called ___.

Front

subqueries

Back

Right outer join example:

Front

SELECT * FROM car_portal_app.a RIGHT JOIN car_portal_app.b ON a_int=b_int;

Back

SQL is a ___ language.

Front

declarative

Back

SQL ___ case sensitive.

Front

is not

Back

SQL statements can contain:

Front

- Keywords determine what exactly it is required from the database to be done - Identifiers refer to the objects in the database—tables, their fields, functions, and so on - Constants are parts of expressions whose values are specified directly in the code - Operators determine how the data is processed in the expressions - Special characters, such as parenthesis, brackets, commas, and so on, which have other meanings than simply being an operator - Whitespaces separate words from each other - Comments are used to describe a particular line of code

Back

Full outer join:

Front

Back

Asterisk (*) represents ___ from all the tables specified in the FROM clause.

Front

all the fields

Back

Scalar subqueries or scalar queries are queries that return exactly ___ column and ___ records.

Front

one, one or zero

Back

It is ___ to use the same name for several columns in a single SELECT query.

Front

possible

Back

conditional expression example

Front

SELECT CASE WHEN now() > date_trunc('day', now()) + interval '12 hours' THEN 'PM' ELSE 'AM' END;

Back

Data Manipulation Language (DML)

Front

used to manage the data itself

Back

Identifiers are the names of the ___.

Front

database objects

Back

It is possible to join a table with itself. It is called ___.

Front

self-join

Back

In general, SQL consists of three parts:

Front

- Data Definition Language (DDL) - Data Manipulation Language (DML) - Data Control Language (DCL)

Back

There are two kinds of syntax of string constants in PostgreSQL:

Front

- single quoted constants like in SQL standard - PostgreSQL-specific dollar-quoted constants

Back

The joining conditions could be specified in the ___ clause but in most cases it is better to put them into a ___ clause to make it more visible that they are here for joining and not for filtering the result of the join, though there is no formal difference.

Front

WHERE, FROM

Back

Full outer join example:

Front

SELECT * FROM car_portal_app.a FULL JOIN car_portal_app.b ON a_int=b_int;

Back

It ___ possible to join more than two tables.

Front

is

Back

Type casts have several syntax patterns with the same meaning:

Front

- CAST (<value> AS <type>) - <value>::<type> - <type> '<value>' (only applicable for string constants) - <type> (<value>)

Back

In many cases, after the rows are taken from the input tables, they should be filtered. It is done via the ___ clause.

Front

WHERE

Back

After the SELECT keyword, one should specify the list of fields or expressions to get from the database. This list is called ___.

Front

Select-list

Back

When DISTINCT is specified, only ___ rows from the input dataset will be returned.

Front

unique

Back

Data Control Language (DCL)

Front

used to control access to the data

Back

Objects such as tables or views can be referred by the name of ___ where it belongs to.

Front

the schema

Back

When records are selected from both tables, we get the ___:

Front

combinations of all their rows

Back

PostgreSQL supports three types of implicitly typed constants: ___, ___ and ___.

Front

numbers, strings, bit strings

Back

SQL commands are terminated by a ___.

Front

semicolon ( ; )

Back

Right outer join:

Front

Back

In general, SELECT queries do not change the data in the database and could be considered as read-only, but there is an exception. If a ___ function is used in the query, then the data can be changed by the function.

Front

volatile

Back

Cross join example:

Front

SELECT * FROM car_portal_app.a CROSS JOIN car_portal_app.b;

Back

Left outer join:

Front

Back

SELECT queries can have different sources:

Front

- tables - views - functions - VALUES command

Back

Left outer join example:

Front

SELECT * FROM car_portal_app.a LEFT JOIN car_portal_app.b ON a_int=b_int;

Back

The JOIN condition may be specified using any of these three ways:

Front

- <first table> JOIN <second table> ON <condition> - <first table> JOIN <second table> USING (<field list>) - <first table> NATURAL JOIN <second table>

Back

Inner join example:

Front

SELECT * FROM car_portal_app.a JOIN car_portal_app.b ON a_int=b_int;

Back

SELECT statements or SELECT queries or just queries are used to ___ the database.

Front

retrieve data from

Back

The logic of SQL implies that the whole algorithm is executed ___, as ___ statement.

Front

all at once, one

Back

If one tries to concatenate a string with a number, PostgreSQL will convert the ___ to a ___ automatically.

Front

number, string

Back

Section 2

(50 cards)

There is a special construct, that can be used to check the equivalence of expressions taking NULL into account:

Front

IS NOT DISTINCT FROM

Back

NULL means the ___.

Front

absence of any value

Back

All set operations by default remove duplicated records as if ___ is used. To avoid this and return all the records, the ___ keyword should be used, which is specified after the name of the set operation.

Front

SELECT DISTINCT, ALL

Back

___ and ___ check for pattern matching using regular expressions.

Front

SIMILAR, ~

Back

HAVING clause is very similar to the WHERE clause but only ___ functions are allowed there.

Front

aggregating

Back

B-tree indexes which are most commonly used, ___ NULL values.

Front

do not index

Back

DEFAULT keyword in the VALUES list example (2 options)

Front

INSERT INTO car_portal_app.a (a_text) VALUES (default); INSERT INTO car_portal_app.a DEFAULT VALUES;

Back

There are three set operations:

Front

- UNION - INTERSECT - EXCEPT

Back

The syntax of the WHERE condition is ___ as in the expressions in the Select-list. This is ___ for PostgreSQL.

Front

the same, specific

Back

It is possible to use several set operations in one statement:

Front

SELECT a, b FROM t1 UNION SELECT c, d FROM t2 INTERSECT SELECT e, f FROM t3;

Back

How to skip several rows at the beginning of the output?

Front

This is done by the using keyword OFFSET.

Back

INSERT INTO ... VALUES example

Front

INSERT INTO car_portal_app.a (a_int) VALUES (6);

Back

NULL = NULL

Front

NULL

Back

To check the expression for having NULL value, a special operator is used:

Front

IS NULL

Back

When subqueries are used ___ they must have an alias.

Front

in the FROM clause

Back

In Oracle EXCEPT is called ___.

Front

MINUS

Back

between example

Front

x BETWEEN a AND b

Back

Only two wildcards can be used in the pattern: ___ for exactly one character (or number) and ___ for any number of any characters, including an empty string.

Front

underscore, percent sign

Back

In contrast to some other databases in PostgreSQL, empty string ___ NULL.

Front

is not

Back

When the records to insert are taken from another table or view, a SELECT query is used instead of the VALUES clause:

Front

INSERT INTO car_portal_app.a SELECT * FROM car_portal_app.b;

Back

The ___ statement is used to insert new data into tables in the database.

Front

INSERT

Back

It ___ possible to refer to the internal elements of one subquery from inside of another.

Front

is not

Back

Subqueries ___ refer to the elements of main query.

Front

can

Back

LIKE and ILIKE are the same except that the first is ___ and second is not.

Front

case-sensitive

Back

By default, the INSERT statement returns the number of inserted records. But it is also possible to return the inserted records themselves, or some of their fields. The output of the statement is then similar to the output of the SELECT query. The RETURNING keyword, with the list of fields to return, is used for this:

Front

INSERT INTO car_portal_app.a SELECT * FROM car_portal_app.b RETURNING a_int;

Back

Sometimes it is necessary to limit the output of the query to a certain number of rows and discard the rest. That is done by specifying that number after the ___ keyword.

Front

LIMIT

Back

The most frequently used aggregating functions:

Front

- count - sum - max - min - avg - corr - stddev - string_agg

Back

group by example

Front

SELECT a.marke, a.model FROM car_portal_app.car_model a INNER JOIN car_portal_app.car b ON a.car_model_id=b.car_model_id GROUP BY a.marke, a.model;

Back

having example

Front

SELECT a.marke, a.model FROM car_portal_app.car_model a INNER JOIN car_portal_app.car b ON a.car_model_id=b.car_model_id GROUP BY a.marke, a.model HAVING count(*)>5;

Back

It is possible to insert multiple records using VALUES syntax:

Front

INSERT INTO car_portal_app.a (a_int, a_text) VALUES (7, 'seven'), (8, 'eight');

Back

The ___ clause is used for grouping.

Front

GROUP BY

Back

In many cases it is required to have the result set sorted. This is done with the ___ clause.

Front

ORDER BY

Back

IS NOT DISTINCT FROM example

Front

SELECT * FROM t WHERE a IS NOT DISTINCT FROM b

Back

The syntax of set operations is as follows:

Front

<query1> UNION <query2>; <query1> INTERSECT <query2>; <query1> EXCEPT <query2>;

Back

To insert several records one should use ___ clause.

Front

VALUES

Back

SQL allows the use of array types that mean several elements as a whole in one single value:

Front

a > ANY (ARRAY[x, y, z])

Back

UNION

Front

This appends the result of one query to the result of another query.

Back

After each item of the ORDER BY list it is possible to specify if the order should be ascending or descending. That is done by specifying the keywords ___ or ___ after the expression.

Front

ASC, DESC

Back

There are some other operators or expressions returning Boolean values that are used in conditional expressions:

Front

- Comparison operators - Pattern matching operators - The OVERLAPS operator - Row and array comparison constructs - Subquery expressions - Any function returning Boolean or convertible to Boolean values

Back

The COALESCE function takes any number of arguments of the same data type or compatible types. It returns the value of ___:

Front

the first of its arguments that is not null

Back

In general, aggregate functions ___ NULL.

Front

ignore

Back

INTERSECT

Front

This returns the records that exist in the results of both queries.

Back

The expression IN is used for checking if a value ___.

Front

equals to any of the values from a list

Back

In fact, in PostgreSQL the VALUES clause is a standalone SQL command. Therefore, it can be used as a subquery in any SELECT query:

Front

SELECT * FROM (VALUES (7, 'seven'), (8, 'eight')) v;

Back

EXCEPT

Front

This returns the records from the first query that do not exist in the result of the second query—the difference.

Back

The difference between them is that SIMILAR uses regular expression syntax defined in ___, while ~ uses ___ regular expressions.

Front

SQL standard, Portable Operating System Interface (POSIX)

Back

The filtering of the groups is done by the ___ clause.

Front

HAVING

Back

NULLIF takes two arguments and returns NULL if ___.

Front

they are equal

Back

The ___ statement is used to change the data in the records of a table without changing their number.

Front

UPDATE

Back

overlaps example

Front

SELECT 1 WHERE (date '2014-10-15', date '2014-10-31') OVERLAPS (date '2014-10-25', date '2014-11-15');

Back

Section 3

(9 cards)

The ___ statement is used to remove records from the database.

Front

DELETE

Back

UPDATE example

Front

UPDATE t SET f = f+1 WHERE a = 5;

Back

TRUNCATE

Front

clears the table completely and almost instantly

Back

It is common to use a subquery in the UPDATE statements. To refer to the updated table from a subquery the table should have an alias:

Front

UPDATE car_portal_app.a u SET a_text = (SELECT b_text FROM car_portal_app.b WHERE b_int = u.a_int);

Back

As well as UPDATE and INSERT , the DELETE statement can return deleted rows when the ___ keyword is used:

Front

RETURNING

Back

Delete based on another tables example:

Front

DELETE FROM car_portal_app.a USING car_portal_app.b WHERE a.a_int=b.b_int;

Back

How to use the FROM clause to update values in a table?

Front

UPDATE car_portal_app.a SET a_int = b_int FROM car_portal_app.b WHERE a.a_text=b.b_text;

Back

DELETE WHERE example:

Front

DELETE FROM car_portal_app.a WHERE a_int IN ( SELECT b_int FROM car_portal_app.b );

Back

TRUNCATE example

Front

TRUNCATE TABLE car_portal_app.a;

Back