SQL Questions II Flashcards

1
Q

What is SQL?

A

It stands for Structured Query Language, and it’s a programming language used for interaction with relational database management systems (RDBMS). This includes fetching, updating, inserting, and removing data from tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are SQL dialects? Give some examples.

A

The various versions of SQL, both free and paid, are also called SQL dialects. All the flavors of SQL have a very similar syntax and vary insignificantly only in additional functionality. Some examples are Microsoft SQL Server, PostgreSQL, MySQL, SQLite, T-SQL, Oracle, and MongoDB.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the main applications of SQL?

A

Using SQL, we can:

-> create, delete, and update tables in a database
-> access, manipulate, and modify data in a table
-> retrieve and summarize the necessary information from a table or several tables
-> add or remove certain rows or columns from a table

All in all, SQL allows querying a database in multiple ways. In addition, SQL easily integrates with other programming languages, such as Python or R, so we can use their combined power.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What types of SQL commands (or SQL subsets) do you know?

A

Data Definition Language (DDL) – to define and modify the structure of a database.

Data Manipulation Language (DML) – to access, manipulate, and modify data in a database.

Data Control Language (DCL) – to control user access to the data in the database and give or revoke privileges to a specific user or a group of users.

Transaction Control Language (TCL) – to control transactions in a database.

Data Query Language (DQL) – to perform queries on the data in a database to retrieve the necessary information from it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a database?

A

A structured storage space where the data is kept in many tables and organized so that the necessary information can be easily fetched, manipulated, and summarized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is DBMS, and what types of DBMS do you know?

A

It stands for Database Management System, a software package used to perform various operations on the data stored in a database, such as accessing, updating, wrangling, inserting, and removing data. There are various types of DBMS, such as relational, hierarchical, network, graph, or object-oriented. These types are based on the way the data is organized, structured, and stored in the system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is RDBMS? Give some examples of RDBMS.

A

It stands for Relational Database Management System. It’s the most common type of DBMS used for working with data stored in multiple tables related to each other by means of shared keys. The SQL programming language is designed to interact with RDBMS. Some examples of RDBMS are MySQL, PostgreSQL, Oracle, MariaDB, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What types of SQL subqueries do you know?

A

Single-row – returns at most one row.
Multi-row – returns at least two rows.
Multi-column – returns at least two columns.
Correlated – a subquery related to the information from the outer query.
Nested – a subquery inside another subquery.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an SQL query, and what types of queries do you know?

A

A query is a piece of code written in SQL to access or modify data from a database.

There are two types of SQL queries: select and action queries. The first ones are used to retrieve the necessary data (this also includes limiting, grouping, ordering the data, extracting the data from multiple tables, etc.), while the second ones are used to create, add, delete, update, rename the data, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a constraint, and why use constraints?

A

A set of conditions defining the type of data that can be input into each column of a table. Constraints ensure data integrity in a table and block undesired actions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What SQL constraints do you know?

A

DEFAULT – provides a default value for a column.
UNIQUE – allows only unique values.
NOT NULL – allows only non-null values.
PRIMARY KEY – allows only unique and strictly non-null values (NOT NULL and UNIQUE).
FOREIGN KEY – provides shared keys between two or more tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a join?

A

A clause used to combine and retrieve records from two or multiple tables. SQL tables can be joined based on the relationship between the columns of those tables. Check out our SQL joins tutorial for more context, plus our dedicated guide to SQL joins interview questions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What types of joins do you know?

A

(INNER) JOIN – returns only those records that satisfy a defined join condition in both (or all) tables. It’s a default SQL join.

LEFT (OUTER) JOIN – returns all records from the left table and those records from the right table that satisfy a defined join condition.

RIGHT (OUTER) JOIN – returns all records from the right table and those records from the left table that satisfy a defined join condition.

FULL (OUTER) JOIN – returns all records from both (or all) tables. It can be considered as a combination of left and right joins.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a primary key?

A

A column (or multiple columns) of a table to which the PRIMARY KEY constraint was imposed to ensure unique and non-null values in that column. In other words, a primary key is a combination of the NOT NULL and UNIQUE constraints. The primary key uniquely identifies each record of the table. Each table should contain a primary key and can’t contain more than one primary key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a unique key?

A

A column (or multiple columns) of a table to which the UNIQUE constraint was imposed to ensure unique values in that column, including a possible NULL value (the only one).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a foreign key?

A

A column (or multiple columns) of a table to which the FOREIGN KEY constraint was imposed to link this column to the primary key in another table (or several tables). The purpose of foreign keys is to keep connected various tables of a database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is an index?

A

A special data structure related to a database table and used for storing its important parts and enabling faster data search and retrieval. Indexes are especially efficient for large databases, where they significantly enhance query performance.

18
Q

What types of indexes do you know?

A

Unique index – doesn’t allow duplicates in a table column and hence helps maintain data integrity.

Clustered index – defines the physical order of records of a database table and performs data searching based on the key values. A table can have only one clustered index.

Non-clustered index – keeps the order of the table records that don’t match the physical order of the actual data on the disk. It means that the data is stored in one place and a non-clustered index – in another one. A table can have multiple non-clustered indexes.

19
Q

What is a schema?

A

A collection of database structural elements such as tables, stored procedures, indexes, functions, and triggers.

It shows the overall database architecture, specifies the relationships between various objects of a database, and defines different access permissions for them. Read our database schema guide for a deeper understanding.

20
Q

What is a SQL comment?

A

A human-readable clarification of what a particular piece of code does. SQL code comments can be single-line (preceded by a double dash –) or span over multiple lines (as follows: /comment_text/). When the SQL engine runs, it ignores code comments. The purpose of adding SQL code comments is to make the code more comprehensive for those people who will read it in the future.

21
Q

What is a SQL operator?

A

A reserved character, a combination of characters, or a keyword used in SQL queries to perform a specific operation. SQL operators are commonly used with the WHERE clause to set a condition (or conditions) for filtering the data.

Arithmetic (+, -, *, /, etc.)
Comparison (>, <, =, >=, etc.)
Compound (+=, -=, *=, /=, etc.)
Logical (AND, OR, NOT, BETWEEN, etc.)
String (%, _, +, ^, etc.)
Set (UNION, UNION ALL, INTERSECT, and MINUS (or EXCEPT))

22
Q

What is an alias?

A

A temporary name given to a table (or a column in a table) while executing a certain SQL query. Aliases are used to improve the code readability and make the code more compact. An alias is introduced with the AS keyword:

23
Q

What are some common statements used with the SELECT query?

A

The most common ones are FROM, GROUP BY, JOIN, WHERE, ORDER BY, LIMIT, and HAVING.

24
Q

What are entities? Give some examples.

A

An entity is a real-world object, creature, place, or phenomenon for which the data can be gathered and stored in a database table.

Each entity corresponds to a row in a table, while the table’s columns describe its properties. Some examples of entities are bank transactions, students in a school, cars sold, etc.

For example, if you use an E-commerce site for purchasing a product, you are represented as an Entity in DBMS, which has attributes such as Customer ID, Name, Country, Phone Number, etc. An** Entity can be** a Tangible Entity (real-world object) for example, a car, or a non-real-world object such as a user account.

25
Q

What are relationships? Give some examples.

A

Relationships are the connections and correlations between entities, basically meaning how two or more tables of a database are related to one another. For example, we can find an ID of the same client in a table on sales data and in a customer table.

26
Q

What is NULL value? How is it different from zero or a blank space?

A

A NULL value indicates the absence of data for a certain cell of a table. Instead, zero is a valid numeric value, and an empty string is a legal string of zero length.

27
Q

What types of SQL functions do you know?

A

Aggregate functions – work on multiple, usually grouped records for the provided columns of a table, and return a single value (usually by group).

Scalar functions – work on each individual value and return a single value.

28
Q

What are case manipulation functions? Give some examples.

A

Case manipulation functions represent a subset of character functions, and they’re used to change the case of the text data. With these functions, we can convert the data into the upper, lower, or title case.

UCASE() (in other SQL flavors – UPPER()) – returns a string converted to the upper case

LCASE() (in other SQL flavors – LOWER()) – returns a string converted to the lower case

INITCAP() – returns a string converted to the title case (i.e., each word of the string starts from a capital letter)

29
Q

What are character manipulation functions? Give some examples.

A

Character manipulation functions represent a subset of character functions, and they’re used to modify the text data.

CONCAT() – joins two or more string values appending the second string to the end of the first one

SUBSTR() – returns a part of a string satisfying the provided start and end points

LENGTH() (in other SQL flavors – LEN()) – returns the length of a string, including the blank spaces

REPLACE() – replaces all occurrences of a defined substring in a provided string with another substring

INSTR() – returns the numeric position of a defined substring in a provided string

LPAD() and RPAD() – return the padding of the left-side/right-side character for right-justified/left-justified value

TRIM() – removes all the defined characters, as well as white spaces, from the left, right, or both ends of a provided string

30
Q

What set operators do you know?

A

UNION – returns the records obtained by at least one of two queries (excluding duplicates)

UNION ALL – returns the records obtained by at least one of two queries (including duplicates)

INTERSECT – returns the records obtained by both queries

EXCEPT (called MINUS in MySQL and Oracle) – returns only the records obtained by the first query but not the second one

31
Q

What is the difference between a primary key and a unique key?

A

While both types of keys ensure unique values in a column of a table, the first one uniquely identifies each record of the table, and the second one prevents duplicates in that column.

32
Q

What is the order of appearance of the common statements in the SELECT query?

A

SELECT – FROM – JOIN – ON – WHERE – GROUP BY – HAVING – ORDER BY – LIMIT

33
Q

In which order the interpreter executes the common statements in the SELECT query?

A

FROM – JOIN – ON – WHERE – GROUP BY – HAVING – SELECT – ORDER BY – LIMIT

34
Q

What is a view, and why use it?

A

A virtual table containing a subset of data retrieved from one or more database tables
(or other views). Views take very little space, simplify complex queries, limit access to the data for security reasons, enable data independence, and summarize data from multiple tables.

35
Q

What types of SQL relationships do you know?

A

One-to-one – each record in one table corresponds to only one record in another table

One-to-many – each record in one table corresponds to several records in another table

Many-to-many – each record in both tables corresponds to several records in another table

36
Q

What is denormalization in SQL, and why use it?

A

Denormalization is the process opposite of normalization: it introduces data redundancy and combines data from multiple tables. Denormalization optimizes the performance of the database infrastructure in situations when read operations are more important than write operations since it helps avoid complex joins and reduces the time of query running.

37
Q

What is the difference between nested and correlated subqueries?

A

A correlated subquery is an inner query nested in a bigger (outer) query that refers to the values from the outer query for its execution, meaning that a correlated subquery depends on its outer query.

opiera się na danych z zewnątrznej subquery

Instead, a non-correlated subquery doesn’t rely on the data from the outer query and can be run independently of it.

38
Q

What is the CASE() function?

A

The way to implement the if-then-else logic in SQL.

This function sequentially checks the provided conditions in the WHEN clauses and returns the value from the corresponding THEN clause when the first condition is satisfied. If none of the conditions is satisfied, the function returns the value from the ELSE clause in case it’s provided, otherwise, it returns NULL.

39
Q

What is the difference between the DELETE and TRUNCATE statements?

A

DELETE is a reversible DML (Data Manipulation Language) command used to delete one or more rows from a table based on the conditions specified in the WHERE clause.

Instead, TRUNCATE is an irreversible DDL (Data Definition Language) command used to delete all rows from a table.

DELETE works slower than TRUNCATE. Also, we can’t use the TRUNCATE statement for a table containing a foreign key.

40
Q

What is the difference between the DROP and TRUNCATE statements?

A

DROP deletes a table from the database completely, including the table structure and all the associated constraints, relationships with other tables, and access privileges.

TRUNCATE deletes all rows from a table without affecting the table structure and constraints. DROP works slower than TRUNCATE. Both are irreversible DDL (Data Definition Language) commands.

41
Q

What is the difference between the HAVING and WHERE statements?

A

The first one works on aggregated data after they are grouped, while the second one checks each row individually. If both statements are present in a query, they appear in the following order: WHERE – GROUP BY – HAVING. The SQL engine interprets them also in the same order.

42
Q
A