SQL Flashcards

1
Q

Explain what SQL is. What are some SQL databases?

A

SQL stands for Structured Query Language and it allows the user to access and manipulate databases.

Some SQL Databases:
MySQL
MariaDB
Oracle
PostgreSQL
MSSQL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 5 sublanguages of SQL? Which commands correspond to each of them?

A

DDL - Data Definition Language: CREATE, ALTER, DROP
DML - Data Manipulation Language: INSERT, UPDATE, DELETE
DQL - Data Query Language: SELECT
DCL - Data Control Language: GRANT, REVOKE
TCL - Transaction Control Language: COMMIT, ROLLBACK, SAVEPOINT

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

What is the difference between DELETE, DROP, and TRUNCATE commands?

A

DELETE (DML) - Used to delete one or more rows of a table

DROP (DDL) - Used to drop a whole table

TRUNCATE (DDL) - Used to delete all rows in a table in one go. Cannot be used with a where clause

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

What are some SQL clauses you can use with SELECT statements?

A

WHERE - Specifies criteria to retrieve from the results of the SELECT statement

HAVING - Filters the results of GROUP BY, HAVING uses the same restrictions as the WHERE clause

GROUP BY - Groups the selected rows based on identical values in a column or expression

ORDER BY - Allows you to specify the columns by which the results table is to be sorted

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

What is the difference between WHERE and HAVING?

A

WHERE - Filters the results of a SELECT statement before GROUP BY can be performed

HAVING - Filters the results of a SELECT statement after GROUP BY is performed

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

Explain what the ORDER BY and GROUP BY clauses do

A

GROUP BY - Groups selected rows that share identical values and can act as a summary of values if using aggregate functions

ORDER BY - Used to sort values by columns or expressions, does not group values

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

Explain the concept of relational integrity

A

Used to ensure accuracy and consistency of data in the relational database. It has three key concepts:

Relational Database - A type of database that stores information in the form of a 2-D table.
Information integrity - The trustworthiness and dependability of information.
Integrity constraints - Sets of rules that can help maintain the quality of information that is acquired

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

List the integrity constraints

A

Entity Integrity Constraints - Constraint which ensure that every entity is unique - Primary Key Constraint, Unique Constraint, Auto_Increment Constraint

Domain Constraints - Constraints which ensure a type of data in a column - Type Constraint, Not null constraint, DEFAULT value Constraint, ENUM constraint

Referential Integrity Constraint - Foreign Key Constraint

User Defined Constraint - CHECK Constraint

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

Define the word “schema”

A

Schema is the structure of a database

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

What is a candidate key? What about a surrogate key?

A

Candidate key is a single or group key that uniquely identifies rows in a table.

Surrogate key- A surrogate key is a system generated (could be GUID, sequence, unique identifier, etc.) value with no business meaning that is used to uniquely identify a record in a table. The key itself could be made up of one or multiple columns (i.e. Composite Key).

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

What conditions lead to orphan records?

A

Its a record whose foreign key value references a non-existent primary key value

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

What are some SQL data types?

A
CHAR
VARCHAR
DATE
TIME
DateTime
INT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is normalization? What are the levels?

A

Normalization is the process to eliminate data redundancy and enhance data integrity in the table. Normalization also helps to organize the data in the database.

1NF: There are only single valued attributes, there is a unique name for each column, order does not matter

2NF: All columns depend on primary key column, partial dependencies placed in separate table

3NF: Non Primary Key columns should not depend on non pk columns

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

What are the properties a transaction must follow?

A

Transaction must follow the ACID properties.

A- Atomicity - By this, we mean that either the entire transaction takes place at once or doesn’t happen at all. There is no midway i.e. transactions do not occur partially. Each transaction is considered as one unit and either runs to completion or is not executed at all.

C-Consistency - This means that integrity constraints must be maintained so that the database is consistent before and after the transaction. It refers to the correctness of a database.

I-Isolation - makes sure that multiple transactions do not interfere with one another.

D- Durability - Make sure that changes in data made by transactions are saved even if a system failure occurs

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

What is the difference between joins and set operators?

A

Set Operators - the columns in the tables must be the same.

Joins - if one column is same its enough to join two or more tables.

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

What are the types of joins? Explain the differences.

A

(INNER) JOIN: Returns records that have matching values in both tables

LEFT JOIN: Returns all records from the left table, and the matched records from the right table

RIGHT JOIN: Returns all records from the right table, and the matched records from the left table

FULL JOIN: Returns all records when there is a match in either left or right table

17
Q

Explain the difference between UNION, UNION ALL, and INTERSECT?

A

Union-combines the results of two queries into a single set of all matching rows. Duplicates removed

Union All- combines two or more results into a single set, including all duplicates.

Intersect-takes the rows from both the result sets which are common in both

18
Q

What is a cascade delete?

A

When we create a foreign key using this option, it deletes the referencing rows in the child table when the referenced row is deleted in the parent table which has a primary key.

19
Q

What is the purpose of a view? What about an index?

A

Views are used to focus, simplify, and customize the perception each user has of the database.

Index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes.

20
Q

What’s the difference between a clustered and non-clustered index?

A

Clustered index is a type of index in which table records are physically reordered to match the index.

A non-clustered index is a in which the logical order of index does not match physical stored order of rows on disk.

21
Q

How would you setup a primary key that automatically increments with every INSERT statement?

A

Give the primary key the auto_increment data type

22
Q

What’s the difference between implicit and explicit cursors?

A

Implicit cursors are automatically created when select statements are executed. Explicit cursors need to be defined explicitly by the user by providing a name. They are capable of fetching a single row at a time. Explicit cursors can fetch multiple rows

23
Q

What is a trigger? Give the syntax for creating a trigger.

A

A trigger is a stored procedure in database which automatically invokes whenever a special event in the database occurs

  1. create trigger [trigger_name]: Creates or replaces an existing trigger with the trigger_name.
  2. {insert | update | delete}: This specifies the DML operation.
  3. on [table_name]: This specifies the name of the table associated with the trigger.
24
Q

What is the difference between scalar and aggregate functions? Give examples of each

A

A scalar function returns a single value. It might not even be related to tables in your database.

  • MID()
  • ROUND()
  • UCASE()
  • LCASE()

An aggregate-valued function returns a calculation across the rows of a table – for example summing values.

  • AVG()
  • COUNT()
  • FIRST()
  • LAST()
  • MAX()
  • MIN()
  • SUM()