Warehouse
Items
What is Data ?
In simple words, data can be facts related to any object in consideration(name, age,
height, weight, a picture, image, file, pdf, etc)
What is Database?
A database is a systematic collection of data. They support electronic storage and
manipulation of data. Databases make data management easy.
An online telephone directory uses a database to store data of people, phone numbers,
and other contact details. Your electricity service provider uses a database to manage billing,
client-related issues, handle fault data, etc.
Let us also consider Facebook. It needs to store, manipulate, and present data related to
members, their friends, member activities, messages, advertisements, and a lot more. We can
provide a countless number of examples for the usage of databases.
What is a DBMS ?
Database Management System (DBMS) is software for storing and
retrieving users’ data while considering appropriate security measures. It consists
of a group of programs that manipulate the database. The DBMS accepts the
request for data from an application and instructs the operating system to provide
the specific data. In large systems, a DBMS helps users and other third-party
software store and retrieve data.
DBMS allows users to create their own databases as per their requirements.
The term “DBMS” includes the user of the database and other application
programs. It provides an interface between the data and the software application.
Types of DBMS
●
●
●
●
●
●
●
●
●
●
●
●
●
●
Hierarchical database
Network databases
Relational databases
Object-oriented databases
Distributed databases
Centralized database
Open-source databases
Cloud databases
Data warehouses
Graph databases
OLTP databases
Personal database
Multimodal database
Document/JSON database
●
●
●
●
●
●
●
●
●
●
●
●
●
●
Hierarchical database
Network databases
Relational databases
Object-oriented databases
Distributed databases
Centralized database
Open-source databases
Cloud databases
Data warehouses
Graph databases
OLTP databases
Personal database
Multimodal database
Document/JSON database
RDBMS
RDBMS stands for Relational Database Management System. RDBMS is a program
used to maintain a relational database. It is also called Relational DBMS, which is the most
popular DBMS type in the market. RDBMS uses SQL queries to access the data in the
database.
●
●
●
●
●
●
●
●
MySQL
Microsoft SQL Server
Oracle
PostgreSQL
SQLite
IBM DB2
MariaDB
dBASE
Database Management System (DBMS) is a software that is used to define, create and maintain
a database and provides controlled access to the data.
Relational Database Management System (RDBMS) is an advanced version of a DBMS.
DBMS and RDBMS difference
DBMS
RDBMS
DBMS stores data as file
RDBMS stores data in tabular form.
Data elements need to access individually.
Multiple data elements can be accessed at the same time.
It deals with small quantity of data.
It deals with large amount of data.
low security levels
multiple levels of data security
It supports single user.
It supports multiple users
Data fetching is slower for the large amount of data.
Data fetching is fast because of relational approach.
Database Management System (DBMS) is a software that is used to define, create and maintain
a database and provides controlled access to the data.
Relational Database Management System (RDBMS) is an advanced version of a DBMS.
Elements of RDBMS ?
●
●
●
●
●
●
●
●
●
Tables
Indexs
Login/group roles
Functions
Triggers
Views
Casts
Types
Sequences
Interview Questions
SQL
What is SQL ?
SQL (pronounced ess-que-ell, not see’qwl) is a language specifically designed with
database. SQL enables people to create databases, add new data to them, maintain the data in
them, and retrieve selected parts of the data using queries. Developed in the 1970s at IBM,
SQL has grown and advanced over the years to become the industry standard.
What is Query ?
A query is a question you ask the database. If any of the data in the database satisfies the
conditions of your query, SQL retrieves that data.
History of SQL ?
SQL originated in one of IBM’s research laboratories, as did relational database theory. In
the early 1970s, as IBM researchers developed early relational DBMS (or RDBMS) systems,
they created a data sublanguage to operate on these systems. They named the pre-release
version of this sublanguage SEQUEL (Structured English QUEry Language). However, when
it came time to formally release their query language as a product, they found that another
company had already trademarked the product name “Sequel.” Therefore, the marketing
geniuses at IBM decided to give the released product a name that was different from SEQUEL
but still recognizable as a member of the same family. So they named it SQL, pronounced
ess-que-ell. Although the official pronunciation is ess-que-ell, people had become accustomed
to pronouncing it “Sequel” in the early pre-release days and continued to do so. That practice
has persisted to the present day; some people will say “Sequel” and others will say “S-Q-L,” but
they are both talking about the same thing.
SQL is not an acronym standing for “structured query language.” It is a sequence of
three letters that don’t stand for anything, just like the name of the C language does not stand
for anything.
History of SQL ?
In 1986, ANSI (the American National Standards Institute) released a formal standard it
named SQL-86. ANSI updated that standard in 1989 to SQL-89 and again in 1992 to SQL-92.
As DBMS vendors proceed through new releases of their products, they try to bring their
implementations ever closer to this standard. This effort has brought the goal of true SQL
portability much closer to reality.
wikipedia
Elements of SQL
Types of SQL Statements
●
●
●
●
●
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Control Language(DCL)
Transaction Control Language(TCL)
Data Query Language (DQL)
What is DDL ?
Data Definition Language helps you to define the database structure or schema. Let’s
learn about DDL commands with syntax.
●
●
●
●
Create
Drop
Alter
Truncate
What is DML ?
Data Manipulation Language (DML) allows you to modify the database instance by
inserting, modifying, and deleting its data. It is responsible for performing all types of data
modification in a database.
●
●
●
Insert
Update
Delete
What is TCL ?
Transaction Control Language or TCL commands deal with the transaction within the
database.
●
●
●
Commit
Rollback
Savepoint
What is DQL ?
Data Query Language (DQL) is used to fetch the data from the database.
●
Select
Interview Questions
PostgreSQL
What is PostgreSQL ?
PostgreSQL is an advanced, enterprise class open source relational database that
supports both SQL (relational) and JSON (non-relational) querying. It is a highly stable database
management system, backed by more than 20 years of community development which has
contributed to its high levels of resilience, integrity, and correctness. PostgreSQL is used as the
primary data store or data warehouse for many web, mobile, geospatial, and analytics
applications.
wikipedia
History of PostgreSQL ?
The PostgreSQL project started in 1986 under the direction of Professor Michael
Stonebreaker at the University of California, Berkeley. The project was originally named
POSTGRES, in reference to the older Ingres database, also developed at Berkeley.
POSTGRES aimed to add the fewest features needed to completely support multiple data types.
In 1996, the project was renamed to PostgreSQL to illustrate its support for the SQL querying
language (although PostgreSQL is still commonly abbreviated as Postgres). A dedicated and
diverse community of contributors – the PostgreSQL Global Development Group – continues to
make regular major and minor releases of the free and open source database project.
What is Schema ?
In PostgreSQL, a schema holds all objects, except for roles and tablespaces. Schemas
effectively act like namespaces, allowing objects of the same name to co-exist in the same
database. By default, newly created databases have a schema called public, but any further
schemas can be added, and the public schema isn't mandatory.
A search_path setting determines the order in which PostgreSQL checks schemas for
unqualified objects (those without a prefixed schema). By default, it is set to $user, public ($user
refers to the currently connected database user). This default can be set on a database or role
level, but as it is a session parameter, it can be freely changed (even multiple times) during a
client session, affecting that session only.
New objects are created in whichever valid schema (one that presently exists) appears
first in the search_path.
What is Tablespace ?
A tablespace is a storage location where the actual data underlying database objects can
be kept. It provides a layer of abstraction between physical and logical data,[1] and serves to
allocate storage for all DBMS managed segments. (A database segment is a database object
which occupies physical space such as table data and indexes.) Once created, a tablespace can
be referred to by name when creating database segments.
Tablespaces specify only the database storage locations, not the logical database
structure, or database schema. For instance, different objects in the same schema may have
different underlying tablespaces. Similarly, a tablespace may service segments for more than
one schema. Sometimes it can be used to specify schema so as to form a bond between logical
and physical data.
wikipedia
Data Types in PostgreSQL
●
●
●
●
●
●
●
●
●
●
Boolean
Arbitrary-precision numerics
Character (text, varchar, char)
Binary
Date/time (timestamp/time with/without time zone, date, interval)
Enum
HStore, an extension enabled key-value store within PostgreSQL
Arrays (variable-length and can be of any data type, including text and composite types)
up to 1 GB in total storage size
Universally unique identifier (UUID)
JavaScript Object Notation (JSON), and a faster binary JSONB
Data types full list
Install PostgreSQL
PostgreSQL Structure
PostgreSQL uses a client/server model. A
PostgreSQL session consists of the following cooperating
processes (programs):
● A server process, which manages the database files,
accepts connections to the database from client
applications, and performs database actions on
behalf of the clients. The database server program is
called postgres.
● The user's client (frontend) application that wants to
perform database operations. Client applications can
be very diverse in nature: a client could be a
text-oriented tool, a graphical application, a web
server that accesses the database to display web
pages, or a specialized database maintenance tool.
Some client applications are supplied with the
PostgreSQL distribution.
Client-server-model-wikipedia
Download Links
Windows Linux
Interview Questions
Table, Insert, Select, Update, Delete and Upsert
Psql commands
psql is a terminal-based front-end to PostgreSQL. It enables you to type in queries
interactively, issue them to PostgreSQL, and see the query results.
●
●
\d table_name -> shows metadata of a table
\dt -> shows tables list of connected database
Tables
The data in an RDBMS is stored in database objects which are called as tables. This table
is basically a collection of related data entries and it consists of numerous columns and rows.
●
●
Rows per table - 4,294,967,295
columns per table - 1600
limits
PostgreSQL Table Structure
CREATE TABLE [IF NOT EXISTS] table_name (
column1 datatype(length) column_constraint,
column2 datatype(length) column_constraint,
column3 datatype(length) column_constraint,
table_constraints
);
Constraints
●
●
●
●
●
NOT NULL – ensures that values in a column cannot be NULL.
UNIQUE – ensures the values in a column unique across the rows within the same table.
PRIMARY KEY – a primary key column uniquely identify rows in a table. A table can have
one and only one primary key. The primary key constraint allows you to define the primary
key of a table.
CHECK – a CHECK constraint ensures the data must satisfy a boolean expression.
FOREIGN KEY – ensures values in a column or a group of columns from a table exists in
a column or group of columns in another table. Unlike the primary key, a table can have
many foreign keys
INSERT statement
Insert is a widely-used command in data manipulation language (DML) used by
relational databases. The insert command is used for inserting one or more rows into a
database table with specified table column values.
INSERT INTO table_name(column1, column2, …) VALUES (value1, value2, …);
column1, column2…
INSERT INTO table_name (column_list) VALUES
(value_list_1),
(value_list_2),
...
(value_list_n)
RETURNING * | output_expression as new_name;
RETURNING
SELECT statement
Select command is used to fetch the data in a set of records from a table, view or a
group of tables, views by making use of SQL joins. Retrieval of data using SQL statements
can be done by using different predicates like − Where. Group By. Having.
SELECT [ ALL | DISTINCT | DISTINCT ON (distinct_expressions) ]
expressions
FROM tables
[WHERE conditions]
[GROUP BY expressions]
[HAVING condition]
[ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS FIRST | NULLS LAST ]]
[LIMIT [ number_rows | ALL]
[OFFSET offset_value [ ROW | ROWS ]]
[FETCH { FIRST | NEXT } [ fetch_rows ] { ROW | ROWS } ONLY]
[FOR { UPDATE | SHARE } OF table [ NOWAIT ]];
UPDATE statement
Update command is a data manipulation command (DML) which is used to edit the
records of a table. It may be used to update a single row based on a condition, all rows or set of
rows based on the condition given by the user.
UPDATE table_name
SET column1 = value1,
column2 = value2,
…
WHERE condition
RETURNING * | output_expression AS output_name;
DELETE statement
Delete command is a data manipulation command(DML) which is used to remove
records from a table. All records may be removed in one go, or a set of records may be deleted
based on a condition.
DELETE FROM table_name
WHERE condition
RETURNING (select_list | *)
UPSERT statement
The term upsert is a portmanteau – a combination of the words “update” and “insert.” In
the context of relational databases, an upsert is a database operation that will update an
existing row if a specified value already exists in a table, and insert a new row if the specified
value doesn't already exist.
INSERT INTO table_name(column_list)
VALUES(value_list)
ON CONFLICT target action;
●
●
target
○ (column_name) - a column name.
○ ON CONSTRAINT constraint_name - where the constraint name could be the name
of the UNIQUE constraint.
○ WHERE predicate - a WHERE clause with a predicate.
action
○ DO NOTHING means do nothing if the row already exists in the table.
○ DO UPDATE SET column_1 = value_1, .. WHERE condition - update some fields in
the table.
Interview Questions
Psql commands
Psql commands
psql is a terminal-based front-end to PostgreSQL. It enables you to type in queries
interactively, issue them to PostgreSQL, and see the query results.
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
\c dbname username -> connects to database
\l -> shows list of databases
\dt -> shows tables list of connected database
\d table_name -> shows metadata of a table
\dn -> shows schema list
\df -> shows functions list
\dv -> shows views list
\du -> shows users list
\g -> executes previous executed query
\s -> history
\? -> help
\! clear
\h ALTER TABLE -> help
\timing
\ef [function name] -> shows functions
\a, \H -> output format
\q -> quit psql
The End
PostgreSQL Constraints
PostgreSQL Constraints
Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the table. If there is any violation between the constraint and
the data action, the action is aborted. Constraints can be column level or table level.
●
●
●
●
●
Primary key
Foreign key
Check constraint
Unique constraint
Not null constraint
Primary key
A primary key is a column or a set of columns in a table whose values uniquely identify a
row in the table. A relational database is designed to enforce the uniqueness of primary keys by
allowing only one row with a given primary key value in a table.
…
PRIMARY KEY (column_1, column_2)
…
Foreign key
A foreign key is a column or columns of data in one table that refers to the unique data
values (often the primary key data) in another table. Foreign keys link together two or more tables
in a relational database.
[CONSTRAINT fk_name]
FOREIGN KEY(fk_columns)
REFERENCES parent_table(parent_key_columns)
[ON DELETE action]
[ON UPDATE action]
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name> FOREIGN KEY(<fk_columns>)
REFERENCES <parent_table>(<parent_tale_columns>)
[ON DELETE <action>]
[ON UPDATE <action>];
●
●
●
●
●
SET NULL
SET DEFAULT
RESTRICT
NO ACTION
CASCADE
Check constraint
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.
●
●
column_name data_type CHECK(...) /* {table}_{column}_check */
column_name data_type CONSTRAINT constraint_name CHECK(...)
●
ALTER TABLE employee
ADD CONSTRAINT constraint_name CHECK (condition1),
ADD CONSTRAINT constraint_name CHECK (condition2);
Unique constraint(Unique Key)
A Unique Key is used to prevent duplicate values in a column. Primary Key provided
uniqueness to a table.
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(column1,column2,..);
NOT NULL constraint
The NOT NULL constraint is used to ensure that a given column of a table is never
assigned the null value. Once a NOT NULL constraint has been defined for a particular column,
any insert or update operation that attempts to place a null value in that column will fail.
ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;
Interview Questions
Data Types in PostgreSQL
Boolean data type
BOOLEAN that can have three values: true, false and NULL.
True
False
true
false
‘t’
‘f ‘
‘true’
‘false’
‘y’
‘n’
‘yes’
‘no’
‘1’
‘0’
Character data types
PostgreSQL provides three primary character types.
●
●
●
CHARACTER(n) or CHAR(n)
CHARACTER VARYINGING(n) or VARCHAR(n)
TEXT
Character Types
Description
CHARACTER VARYING(n), VARCHAR(n)
variable-length with length limit
CHARACTER(n), CHAR(n)
fixed-length, blank padded
TEXT
variable unlimited length
Numeric data type
Numeric types consist of two-, four-, and eight-byte integers, four- and eight-byte
floating-point numbers, and selectable-precision decimals.
documentation
DATE data type
PostgreSQL uses 4 bytes to store a date value. The lowest and highest values of the
DATE data type are 4713 BC and 5874897 AD.
When storing a date value, PostgreSQL uses the yyyy-mm-dd format (2000-12-31). It
also uses this format for inserting data into a date column.
PostgreSQL DATE functions
●
●
●
●
●
Get the current date
Output a PostgreSQL date value in a specific format(TO_CHAR)
Get the interval between two dates(INTERVAL)
Calculate ages in years, months, and days(AGE)
Extract year, quarter, month, week, day from a date value(EXTRACT)
Interval data type
The interval data type allows you to store and manipulate a period of time in years,
months, days, hours, minutes, seconds, etc.
Intervalstyle => [sql_standard, postgres, postgres_verbose, iso_8601]
documentation
timestamp data types
PostgreSQL provides you with two temporal data types for handling timestamp:
●
●
timestamp: a timestamp without timezone one.
timestamptz: timestamp with a timezone.
The timestamp data type allows you to store both date and time. However, it does not
have any time zone data. It means that when you change the timezone of your database server,
the timestamp value stored in the database will not change automatically.
The timestamptz datatype is the timestamp with the time zone. The timestamptz datatype
is a time zone-aware date and time data type. Both timestamp and timestamptz uses 8 bytes for
storing the timestamp
●
●
●
●
●
●
●
SELECT NOW();
SELECT CURRENT_TIMESTAMP;
SHOW timezone
SET timezone;
pg_timezone_names;
SELECT TIMEOFDAY();
SELECT timezone(tz, timezone);
wikipedia
TIME data type
TIME data type allows to store the time of day values.
●
●
●
●
●
HH:MI:SS(00:00:00)
SELECT CURRENT_TIME(precision);
○
0 <= precision <= 6
SELECT LOCALTIME(precision);
○
0 <= precision <= 6
SELECT LOCALTIME AT TIME ZONE 'UTC-5';
column TIME with time zone;
UUID data type
A UUID value is 128-bit quantity generated by an algorithm that make it unique in the
known universe using the same algorithm. The following shows some examples of the UUID
values:
●
●
●
●
●
select gen_random_uuid();
select * from pg_extension;
CREATE EXTENSION "uuid-ossp";
select uuid_generate_v1();
select uuid_generate_v4();
ARRAY data type
Array plays an important role in PostgreSQL. Every data type has its own companion
array type e.g., integer has an integer[] array type, character has character[] array type, etc.
PostgreSQL allows you to define a column to be an array of any valid data type including
built-in type, user-defined type or enumerated type.
●
unnest()
HSTORE data type
The hstore module implements the hstore data type for storing key-value pairs in a single
value. The hstore data type is very useful in many cases, such as semi-structured data or rows
with many attributes that are rarely queried.
●
●
●
●
●
●
●
●
●
●
●
●
●
CREATE EXTENSION hstore;
select column ->'key' … ,
… SET column = column || '"key"=>"value"' :: hstore;
… SET column = delete(column, 'key');
… WHERE column ? 'key';
… WHERE column @> '”key”=>”value”';
… WHERE ?& ARRAY [ 'key1', 'key2' ];
… WHERE ?| ARRAY [ 'key1', 'key2' ];
akeys(column)
skeys(column)
avals (attr)
svals (attr)
hstore_to_json (column)
JSON data type
JSON stands for JavaScript Object Notation. JSON is an open standard format that
consists of key-value pairs.
The main usage of JSON is to transport data between a server and a web application.
Unlike other formats, JSON is human-readable text. PostgreSQL supports native JSON data
type since version 9.2. It provides many functions and operators for manipulating JSON data.
●
operators (->, ->>)
Custom data types
It is possible to create custom data type using two ways using CREATE DOMAIN and
CREATE TYPE statement.
●
●
CREATE DOMAIN creates a user-defined data type with constraints such as NOT NULL,
CHECK, PATTERN MATCHING etc.
CREATE TYPE creates a composite type used in stored procedures as the data types of
returned values.
Interview Questions
Managing Tables
Create table
A primary key is a column or a set of columns in a table whose values uniquely identify a
row in the table. A relational database is designed to enforce the uniqueness of primary keys by
allowing only one row with a given primary key value in a table.
CREATE TABLE [IF NOT EXISTS] table_name (
column1 datatype(length) column_contraint,
column2 datatype(length) column_contraint,
column3 datatype(length) column_contraint,
table_constraints
);
To copy a table completely, including both table structure and data;
●
CREATE TABLE new_table AS TABLE existing_table [WITH NO DATA];
Sequence
A sequence is a special kind of database object that generates a sequence of integers. A
sequence is often used as the primary key column in a table. The sequence can be created
through the SERIAL pseudo-type as follows.
●
●
CREATE SEQUENCE [ IF NOT EXISTS ] sequence_name
[ AS { SMALLINT | INT | BIGINT } ]
[ INCREMENT [ BY ] increment ]
[ MINVALUE minvalue | NO MINVALUE ]
[ MAXVALUE maxvalue | NO MAXVALUE ]
[ START [ WITH ] start ]
[ CACHE cache ]
[ [ NO ] CYCLE ]
[ OWNED BY { table_name.column_name | NONE } ]
DROP SEQUENCE [ IF EXISTS ] sequence_name [, ...]
[ CASCADE | RESTRICT ];
IDENTITY
PostgreSQL version 10 introduced a new constraint GENERATED AS IDENTITY that
allows you to automatically assign a unique number to a column.
The GENERATED AS IDENTITY constraint is the SQL standard-conforming variant of the
good old SERIAL column.
●
●
column_name
type
GENERATED
sequence_option )]
OVERRIDING SYSTEM VALUE
{
ALWAYS
|
BY
DEFAULT
}
AS
IDENTITY[(
ALTER table
ALTER TABLE statement used to change the structure of an existing table
●
●
●
●
●
●
●
●
●
●
ALTER TABLE table_name action;
ALTER TABLE table_name ADD COLUMN column_name datatype column_constraint;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name RENAME COLUMN column_name TO new_column_name;
ALTER TABLE table_name ALTER COLUMN column_name [SET DEFAULT value | DROP
DEFAULT];
ALTER TABLE table_name ALTER COLUMN column_name [SET NOT NULL| DROP NOT NULL];
ALTER TABLE table_name ADD CHECK expression;
ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_definition;
ALTER TABLE table_name [IF EXISTS] RENAME TO new_table_name;
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type USING
expression;
TRUNCATE table
TRUNCATE TABLE statement used to delete all data from a table and very efficient
comparing to DELETE statement.
●
TRUNCATE TABLE table_name [RESTART IDENTITY] [CASCADE];
TEMPORARY TABLE statement
A temporary table, as its name implied, is a short-lived table that exists for the duration of a
database session. PostgreSQL automatically drops the temporary tables at the end of a session
or a transaction.
●
CREATE TEMPORARY TABLE temp_table_name( column_list);
Interview Questions
Querying Data
Column alias
A column alias allows you to assign a column or an expression in the select list of a
SELECT statement a temporary name. The column alias exists temporarily during the execution
of the query.
SELECT column_name AS new_name
FROM table_name;
ORDER BY clause
When you query data from a table, the SELECT statement returns rows in an unspecified
order. To sort the rows of the result set, you use the ORDER BY clause in the SELECT statement.
The ORDER BY clause allows you to sort rows returned by a SELECT clause in ascending
or descending order based on a sort expression.
SELECT
select_list
FROM
table_name
ORDER BY
sort_expression1 [ASC | DESC],
...
sort_expressionN [ASC | DESC];
ORDER BY sort_expresssion [ASC | DESC] [NULLS FIRST | NULLS LAST]
DISTINCT clause
The DISTINCT clause is used in the SELECT statement to remove duplicate rows from a
result set. The DISTINCT clause keeps one row for each group of duplicates. The DISTINCT
clause can be applied to one or more columns in the select list of the SELECT statement.
SELECT
DISTINCT column1
FROM
table_name;
Interview Questions
Data Filtering
Where clause
The SELECT statement returns all rows from one or more columns in a table. To select rows
that satisfy a specified condition, you use a WHERE clause.
SELECT columns...
FROM table_name
WHERE condition
ORDER BY sort_expression
Operator
Description
=
Equal
>
Greater than
<
Less than
>=
Greater than or equal
<=
Less than or equal
<> or !=
Not equal
AND
Logical operator AND
OR
Logical operator OR
IN
Return true if a value matches any value in a list
BETWEEN
Return true if a value is between a range of values
LIKE
Return true if a value matches a pattern
IS NULL
Return true if a value is NULL
NOT
Negate the result of other operators
IN operator
The IN operator allows you to specify multiple values in a WHERE clause. The IN operator
is a shorthand for multiple OR conditions.
value IN (value1,value2,...)
value IN (SELECT column_name FROM table_name);
BETWEEN operator
The BETWEEN operator allows you to easily test if an expression is within a range of values
(inclusive). The values can be text, date, or numbers.
value BETWEEN low AND high;
value NOT BETWEEN low AND high;
LIKE operator
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.
value LIKE pattern;
value NOT LIKE pattern
ILIKE operator
The ILIKE operator that works like the LIKE operator in addition ILIKE operator matches
value case-insensitively.
value ILIKE pattern;
value NOT ILIKE pattern
Interview Questions
Nested SELECT
(ANY, ALL and EXISTS operators)
Nested SELECT
A subquery is a query that appears inside another query statement. Subqueries are also
referred to as sub SELECT s or nested SELECT s;
ANY operator
ANY means that the condition will be true if the operation is true for any of the values in the
range.
SELECT column_name(s)
FROM table1
WHERE column_name expression operator ANY(subquery);
●
●
●
The subquery must return exactly one column.
Expression operator must be one of the those operators =, <=, >, <, > and <>
The ANY operator returns true if any value of the subquery meets the condition, otherwise, it
returns false.
ALL operator
The ALL operator: returns a boolean value as a result. returns TRUE if ALL of the subquery
values meet the condition. is used with SELECT , WHERE and HAVING statements.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL(....)
●
The operator must be one of operators (=, <>, !=, >, >=, <, <=)
EXISTS operator
The EXISTS operator is used to test for the existence of any record in a subquery. The
EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
Interview Questions
Build in Functions
Date functions
●
●
●
●
●
●
●
●
●
AGE
EXTRACT
CURRENT_DATE
CURRENT_TIME(precision)
CURRENT_TIMESTAMP(precision)
LOCALTIME(precision)
LOCALTIMESTAMP(precision)
TO_DATE(text,pattern)
TO_TIMESTAMP(text,pattern)
Date Time functions documentation
Date Time formatting documentation
String functions
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
ASCII
CHR
CONCAT, ||, CONCAT_WS
FORMAT
INITCAP
LEFT
LENGTH
LOWER
UPPER
POSITION
TRIM
REPEAT
REVERSE
RTRIM
LTRIM
SUBSTRING
Postgresql documentation
Math functions
●
●
●
●
●
●
●
●
●
●
●
ABS
CBRT
CEIL
FLOOR
LN
LOG
PI
SQRT
RANDOM
TRUNC
POWER
Postgresql documentation
Pattern Matching
Postgresql documentation
Data Grouping
GROUP BY Statement
The GROUP BY Statement in SQL is used to arrange identical data into groups with the
help of some functions. if a particular column has same values in different rows then it will arrange
these rows in a group.
SELECT
column_1,
column_2,
...,
aggregate_function(column_3)
FROM
table_name
GROUP BY
column_1,
column_2,
...;
Having clause
Having clause is used to filter data according to the conditions provided. Having clause is
generally used in reports of large data. Having clause is only used with the SELECT clause. The
expression in the syntax can only have constants.
SELECT
column1,
aggregate_function (column2)
FROM
table_name
GROUP BY
column1
HAVING
condition;
Interview Questions
Union, Intersect and Except
Union Statement
The PostgreSQL UNION operator is used for combining result sets from more than one SELECT
statement into one result set. Any duplicate rows from the results of the SELECT statements are
eliminated. The UNION operator works under two conditions:
●
●
The SELECT queries MUST return a similar number of queries.
The data types of all corresponding columns must be compatible.
SELECT expression_1, expression_2, ... expression_n
FROM tables
[WHERE condition(s)]
UNION
SELECT expression_1, expression_2, ... expression_n
FROM tables
[WHERE condition(s)];
Note : The fields to be used in both the select statements must be in same order, same number
and same data type.
INTERSECT clause
The INTERSECT clause in SQL is used to combine two SELECT statements but the dataset
returned by the INTERSECT statement will be the intersection of the data-sets of the two
SELECT statements.
SELECT select_list
FROM A
INTERSECT
SELECT select_list
FROM B;
EXCEPT clause
The EXCEPT clause is used to combine two SELECT statements and returns rows from the
first SELECT statement that are not returned by the second SELECT statement.
SELECT select_list
FROM A
EXCEPT
SELECT select_list
FROM B;
Interview Questions
Views
View
A view is a named query that provides another way to present data in the database tables. A
view is defined based on one or more tables which are known as base tables. When you create a
view, you basically create a query and assign a name to the query. Therefore, a view is useful for
wrapping a commonly used complex query.
Note that regular views do not store any data except the materialized views. In
PostgreSQL, you can create special views called materialized views that store data physically
and periodically refresh data from the base tables. The materialized views are handy in many
scenarios, such as faster data access to a remote server and caching.
● Standard Views.
● Materialized Views.
Postgresql documentation
Standard Views
A view is a database object that is of a stored query. A view can be accessed as a virtual
table in PostgreSQL. In other words, a PostgreSQL view is a logical table that represents data of
one or more underlying tables through a SELECT statement. Notice that a view does not store
data physically.
●
●
●
●
CREATE VIEW view_name AS query;
ALTER VIEW old_name RENAME TO new_name;
DROP VIEW [IF EXISTS] view_name [CASCADE | RESTRICT]
DROP VIEW [ IF EXISTS ] view1, view2, ...;
Materialized views
PostgreSQL extends the view concept to the next level that allows views to store data
physically. And these views are called materialized views.
Materialized views cache the result of a complex and expensive query and allow you to
refresh this result periodically.
●
●
●
●
CREATE MATERIALIZED VIEW view_name AS query WITH [NO] DATA;
REFRESH MATERIALIZED VIEW view_name;
REFRESH MATERIALIZED VIEW CONCURRENTLY view_name; –- from postgresql 9.4
DROP MATERIALIZED VIEW view_name;
Interview Questions
PL/pgSQL
PL/pgSQL
PL/pgSQL (Procedural Language/PostgreSQL) is a procedural programming language
supported by the PostgreSQL. Implemented by Jan Wieck, PL/pgSQL first appeared with
PostgreSQL 6.4, released on October 30, 1998. Since PostgreSQL 9.0, PL/pgSQL is installed
by default. PL/pgSQL, as a fully featured programming language, allows much more
procedural control than SQL, including the ability to use loops and other control structures. SQL
statements and triggers can call functions created in the PL/pgSQL language. The design of
PL/pgSQL aimed to allow PostgreSQL users to perform more complex operations and
computations than SQL, while providing ease of use.
PL/pgSQL procedural language is similar to the Oracle PL/SQL. The following are reasons
to learn PL/pgSQL.
● PL/pgSQL comes with PostgreSQL by default.
● PL/pgSQL inherits all user-defined types, functions, and operators.
● PL/pgSQL has many features that allow you to develop complex functions and stored
procedures.
wikipedia
Dollar-Quoted string constant
●
●
select 'String constant';
select 'I’m a string constant';
●
●
select 'I''m also a string constant';
select E'I\'m also a string constant';
The problem arises when the string constant contains many single quotes and backslashes.
Doubling every single quote and backslash makes the string constant more difficult to read and
maintain.
PostgreSQL version 8.0 introduced the dollar quoting feature to make string constants
more readable.
$tag$<string_constant>$tag$
Anonymous block
PL/pgSQL is a block-structured language, a PL/pgSQL function or stored procedure is
organized into blocks.
[ <<label>> ]
[ declare
declarations ]
begin
statements;
...
end [ label ];
To execute anonymous block use DO. The DO statement was introduced since PostgreSQL 9.0.
DO $$
<<fb>>
declare
begin
raise info 'Hello PDP';
end fb;
$$;
Interview Questions
Variables and Constant
Variables
A variable is a meaningful name of a memory location. A variable holds a value that can be
changed through the block. A variable is always associated with a particular data type. Before
using a variable,variables must be declared in the declaration section of the PL/pgSQL block.
variable_name data_type [:= expression];
select into statement
The SELECT INTO statement is a query that allows to set value or values into a variable.
select column(s) into variable from . . .;
Use rowtype variables (%ROWTYPE) to hold a row of a result set returned by the select into
statement.
variable table_name%ROWTYPE;
variable view_name%ROWTYPE;
Record Type
Record type is similar to the row-type. But with little difference, a record variable does not
have a predefined structure. The structure of a record variable is determined when the select or
for statement assigns an actual row to it.
variable record;
constants
The value of a constant cannot be changed once it initialized.
constant_name constant data_type := expression;
Interview Questions
PL/pgSQL (Control Statements)
IF statement
IF statement let you execute alternative commands based on certain conditions. PL/pgSQL
has three forms of IF.
●
●
●
IF ... THEN ... END IF
IF ... THEN ... ELSE ... END IF
IF ... THEN ... ELSIF ... THEN ... ELSE ... END IF
Postgresql documentation
CASE statement
CASE search-expression
WHEN expression [, expression [ ... ]] THEN
statements
[ WHEN expression [, expression [ ... ]] THEN
statements
... ]
[ ELSE
statements ]
END CASE;
Postgresql documentation
Simple Loop statement
LOOP
-- some computations
IF count > 0 THEN
EXIT; -- exit loop
END IF;
END LOOP;
LOOP
-- some computations
EXIT WHEN count > 0; -- same result as previous example
END LOOP;
<<ablock>>
BEGIN
-- some computations
IF stocks > 100000 THEN
EXIT ablock; -- causes exit from the BEGIN block
END IF;
-- computations here will be skipped when stocks > 100000
END;
Postgresql documentation
CONTINUE statement
CONTINUE [ label ] [ WHEN boolean-expression ];
LOOP
-- some computations
EXIT WHEN count > 100;
CONTINUE WHEN count < 50;
-- some computations for count IN [50 .. 100]
END LOOP;
Postgresql documentation
WHILE statement
[ <<label>> ]
WHILE boolean-expression LOOP
statements
END LOOP [ label ];
Postgresql documentation
FOR statement
For (integer numbers)
[ <<label>> ]
FOR name IN [ REVERSE ] expression .. expression [ BY expression ] LOOP
statements
END LOOP [ label ];
Looping through Query Results
[ <<label>> ]
FOR target IN query LOOP
statements
END LOOP [ label ];
Looping through Arrays
[ <<label>> ]
FOREACH target [ SLICE number ] IN ARRAY expression LOOP
statements
END LOOP [ label ];
Postgresql documentation
Interview Questions
Reporting Messages
Reporting Messages
RAISE is used to raise errors and report messages, PostgreSQL provides various
parameters to report an error, warning, and information at a detailed level. Below is the basic
syntax for the RAISE command.
raise level format;
●
●
level
○
debug
○
log
○
notice
○
info
○
warning
○
exception(default)
format
○
%
FUSE Github
Note: raise statement uses the exception level by default.
Raising errors
Besides raising an error, you can add more information by using the following additional
clause:
●
●
raise exception ‘message’ using option = expression;
option
○
message: set error message
○
hint: provide the hint message so that the root cause of the error is easier to be discovered.
○
detail: give detailed information about the error.
○
errcode: identify the error code, which can be either by condition name or directly five-character
SQLSTATE code.
raise sqlstate ‘five-character’;
sqlstate documentation
Trap Errors
By default, any error occurring in a PL/pgSQL function aborts execution of the function and
the surrounding transaction. You can trap errors and recover from them by using a BEGIN block
with an EXCEPTION clause.
[ <<label>> ]
[ DECLARE
declarations ]
BEGIN
statements
EXCEPTION
WHEN condition [ OR condition ... ] THEN
handler_statements
[ WHEN condition [ OR condition ... ] THEN
handler_statements
... ]
WHEN OTHERS THEN
handler_statements
END;
WHEN division_by_zero THEN ...
WHEN SQLSTATE '22012' THEN ...
A block containing an EXCEPTION clause is significantly more expensive to enter and exit
than a block without one. Therefore, don't use EXCEPTION without need
Postgresql
documentation
Obtaining Information About An Error
Within an exception handler, one may also retrieve information about the current exception
by using the GET STACKED DIAGNOSTICS command.
GET STACKED DIAGNOSTICS variable { = | := } item [ , ... ];
Postgresql documentation
Assert Statement
The assert statement is a useful shorthand for inserting debugging checks into PL/pgSQL
code(Since postgreSQL 9.5).
assert condition [, message];
show plpgsql.check_asserts;
set plpgsql.check_asserts = [on/off];
Note: you should use the assert statement solely for detecting bugs, not for reporting. To
report a message or an error, you use the raise statement instead.
Interview Questions
PL/pgSQL (User-defined functions and Procedures)
Create Function
create [or replace] function function_name(param_list)
returns return_type
language plpgsql
as
$$
declare
-- variable declaration
begin
-- logic
end;
$$
Function Parameter Modes
The parameter modes determine the behaviors of parameters. PL/pgSQL supports three
parameter modes: in, out, and inout. A parameter takes the in mode by default if you do not
explicitly specify it.
IN
OUT
INOUT
The default
Explicitly specified
Explicitly specified
Pass a value to function
Return a value from a function
Pass a value to a function and
return an updated value.
in parameters act like
constants
out parameters act like
uninitialized variables
inout parameters act like an
initialized variables
Cannot be assigned a value
Must assign a value
Should be assigned a value
Function overloading
PostgreSQL allows multiple functions to share the same name as long as they have
different arguments. If two or more functions share the same name, the function names are
overloaded.
When you can call an overloading function, PostgreSQL select the best candidate function
to execute based on the the function argument list.
Note: Parenthesized type modifiers are discarded by CREATE FUNCTION.
Postgresql documentation
Returning table
create [or replace] function function_name(parameter(s))
returns table ( column(s) )
language plpgsql
as $$
declare
-- variable declaration
begin
-- body
end; $$
Drop function
drop function [if exists] function_name(argument(s)) [cascade | restrict]
Procedures
A drawback of user-defined functions is that they cannot execute transactions. In other
words, inside a user-defined function, you cannot start a transaction, and commit or rollback it.
PostgreSQL 11 introduced stored procedures that support transactions. To define a new
stored procedure, you use the create procedure statement.
create [or replace] procedure procedure_name(parameter(s))
language plpgsql
as $$
declare
-- variable declaration
begin
-- stored procedure body
end; $$
Drop procedure
drop procedure [if exists] procedure_name (argument(s)) [cascade | restrict]
Interview Questions
PL/pgSQL (Trigger)
CREATE Trigger
●
CREATE FUNCTION trigger_function()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
-- trigger logic
END;
$$
●
CREATE TRIGGER trigger_name
{BEFORE | AFTER} { event }
ON table_name
[FOR [EACH] { ROW | STATEMENT }]
EXECUTE PROCEDURE trigger_function
DROP TRIGGER [IF EXISTS] trigger_name ON table_name [ CASCADE | RESTRICT ];
ALTER TRIGGER trigger_name ON table_name RENAME TO new_trigger_name;
ALTER TABLE table_name DISABLE TRIGGER trigger_name | ALL;
ALTER TABLE table_name ENABLE TRIGGER trigger_name | ALL;
●
●
●
●
Interview Questions
Transaction and ACID
ACID
In the context of transaction processing, the acronym ACID refers to the four key properties
of a transaction: atomicity, consistency, isolation, and durability.
Atomicity
Atomicity means that you guarantee that either all of the transaction succeeds or none of it
does. You don’t get part of it succeeding and part of it not. If one part of the transaction fails, the
whole transaction fails. With atomicity, it’s either “all or nothing”.
Consistency
This ensures that you guarantee that all data will be consistent. All data will be valid
according to all defined rules, including any constraints, cascades, and triggers that have been
applied on the database.
Isolation
Guarantees that all transactions will occur in isolation. No transaction will be affected by any
other transaction. So a transaction cannot read data from any other transaction that has not yet
completed.
Durability
Durability means that, once a transaction is committed, it will remain in the system – even if
there’s a system crash immediately following the transaction. Any changes from the transaction
must be stored permanently. If the system tells the user that the transaction has succeeded, the
transaction must have, in fact, succeeded.
wikipedia
Interview Questions
Transactions
Transactions
A database transaction is a single unit of work that consists of one or more operations. A
classical example of a transaction is a bank transfer from one account to another. A complete
transaction must ensure a balance between the sender and receiver accounts. It means that if the
sender account transfers X amount, the receiver receives X amount, no more or no less.
A PostgreSQL transaction is atomic, consistent, isolated, and durable. These properties are
often referred to as ACID.
BEGIN; -- BEGIN TRANSACTION; -- BEGIN WORK;
-- query
ROLLBACK;
COMMIT; -- COMMIT TRANSACTION; -- COMMIT WORK;
Interview Questions
Joining Tables
Joining Tables
In DBMS, a join statement is mainly used to combine two or more tables based on a
specified common field between them.
PostgreSQL supports inner join, left join, right join, full outer join, cross join, natural
join, and a special kind of join called self-join.
INNER JOIN clause
Inner Join clause creates a new table (not physical) by combining rows that have
matching values in two or more tables. This join is based on a logical relationship (or a common
field) between the tables and is used to retrieve data that appears in both tables.
SELECT *
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;
LEFT JOIN clause
The LEFT JOIN clause returns all records from the left table , and the matching records
from the right table.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT JOIN clause
The RIGHT JOIN clause returns all records from the right table, and the matching records
from the left table.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
NATURAL JOIN clause
A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on
the common columns in the two tables being joined. Common columns are columns that have the
same name in both tables. A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a
RIGHT OUTER join. The default is INNER join.
select * from table_one natural join table_two;
Interview Questions
Indexes
Indexes
PostgreSQL indexes are effective tools to enhance database performance. Indexes help the
database server find specific rows much faster than it could do without indexes.
Indexes add write and storage overheads to the database system. Therefore, using them
appropriately is very important.
●
●
SELECT * FROM pg_indexes;
CREATE INDEX index_name ON table_name [USING method(btree, hash, gist, spgist,
gin, and brin)]
(
column_name [ASC | DESC] [NULLS {FIRST | LAST }],
);
Postgresql documentation
Unique Indexes
UNIQUE index enforces the uniqueness of values in one or multiple columns.
●
●
CREATE UNIQUE INDEX index_name ON table_name(column_name, [...]);
DROP INDEX [ CONCURRENTLY ] [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
Interview Questions
Roles & Privileges
ROLES
PostgreSQL uses roles to represent user accounts. It doesn’t use the user concept like
other database systems. Typically, roles can log in are called login roles. They are equivalent to
users in other database systems. When roles contain other roles, they are called group roles.
●
●
●
SELECT * FROM pg_roles;
CREATE ROLE name [ [ WITH ] option [ ... ] ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| LOGIN | NOLOGIN
| CONNECTION LIMIT connlimit
| [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL
| VALID UNTIL 'timestamp'
documentation
GRANT
After creating a role with the LOGIN attribute, the role can log in to the PostgreSQL
database server. However, it cannot do anything to the database objects like tables, views,
functions, etc.
Need to be granted privileges on the database objects to the user role by using the GRANT
statement.
●
GRANT privilege_list | ALL
ON table_name
TO role_name;
Postgresql documentation
REVOKE
The REVOKE statement revokes previously granted privileges on database objects from a
role.
●
REVOKE privilege | ALL
ON TABLE table_name | ALL TABLES IN SCHEMA schema_name
FROM role_name;
Postgresql documentation
DROP ROLE
●
DROP ROLE [IF EXISTS] target_role;
Postgresql documentation
Interview Questions
Tablespaces
Tablespace
A tablespace is a location on the disk where PostgreSQL stores data files containing database
objects e.g., indexes, and tables.
PostgreSQL uses a tablespace to map a logical name to a physical location on disk.
PostgreSQL comes with two default tablespaces:
● pg_default tablespace stores user data.
● pg_global tablespace stores global data.
Tablespaces allow you to control the disk layout of PostgreSQL.
●
CREATE TABLESPACE tablespace_name OWNER user_name LOCATION directory_path;
chown postgres:postgres /location
postgresql documentation
ALTER Tablespace
●
ALTER TABLESPACE tablespace_name action;
○
ALTER TABLESPACE old_tablespace_name RENAME TO new_tablespace_name;
○
ALTER TABLESPACE tablespace_name OWNER to user_name;
DROP Tablespace
●
●
DROP TABLESPACE table_space;
ALTER DATABASE test_database SET TABLESPACE pg_default;
Interview Questions
Database and Schema
Create DataBase
CREATE DATABASE cannot be executed inside a transaction block. Although it is possible to
copy a database other than template1 by specifying its name as the template, this is not (yet)
intended as a general-purpose “COPY DATABASE” facility. The principal limitation is that no other
sessions can be connected to the template database while it is being copied. CREATE
DATABASE will fail if any other connection exists when it starts; otherwise, new connections to the
template database are locked out until CREATE DATABASE completes.
postgresql documentation
Alter Database
ALTER DATABASE name [ [ WITH ] option [ ... ] ]
where option can be:
ALLOW_CONNECTIONS allowconn
CONNECTION LIMIT connlimit
IS_TEMPLATE istemplate
ALTER DATABASE name RENAME TO new_name
ALTER DATABASE name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER
SESSION_USER }
ALTER DATABASE name SET TABLESPACE new_tablespace
ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE name SET configuration_parameter FROM CURRENT
ALTER DATABASE name RESET configuration_parameter
ALTER DATABASE name RESET ALL
|
postgresql documentation
Schema
A database contains one or more named schemas, which in turn contain tables. Schemas
also contain other kinds of named objects, including data types, functions, and operators. The
same object name can be used in different schemas without conflict; for example, both schema1
and myschema can contain tables named mytable. Unlike databases, schemas are not rigidly
separated: a user can access objects in any of the schemas in the database they are connected
to, if they have privileges to do so.
There are several reasons why one might want to use schemas:
● To allow many users to use one database without interfering with each other.
● To organize database objects into logical groups to make them more manageable.
● Third-party applications can be put into separate schemas so they do not collide with the
names of other objects.
CREATE SCHEMA myschema;
show search_path;
set search_path to xyz;
information_schema
pg_catalog;
postgresql documentation
Alter Schema
ALTER SCHEMA name RENAME TO new_name
ALTER SCHEMA name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
postgresql documentation
Interview Questions
Backup & Restore with pgadmin
Using DATAGRIP
Backup & Restore with datagrip
Interview Questions
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )