SQL Flashcards

1
Q

How do you declare a variable in TSQL?

A

You declare a variable in TSQL by starting with the keyword “Declare”. Then, you provide the name you choose to use preceded by the “@” symbol. Finally, you must also assign the datatype to the variable.

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

How many normal forms are there?

A

Normal Forms are a set of rules or guidelines used in relational database design for organizing and structuring data in a way that minimizes redundancy, dependency, and the potential for data anomalies.
There are 3 main normal forms:
First Normal Form (1NF) –
* Must contain a Primary Key
* No duplicated rows or columns
* Each column must have only 1 value for each row in the table
* One cell cannot hold more than one value
Second Normal Form (2NF) –
* Meets 1NF standards
* All non-key attributes are fully dependent on a PK.
Third Normal Form (3NF) – No non-key attributes is dependent on another non-key attribute

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

If a stored procedure is too slow, how can you enhance the speed?

A

Create indexes on frequently used columns used in search or filter conditions
Make sure the sql queries in the stored proc are well-organized

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

If you wanted to delete information from a table, what statement would you use?

A

“Delete” statement to delete specific records based on conditions.

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

What are indexes?

A

They are data structures in a database that provide a way to efficiently retrieve and access data. They are similar to bookmarks because they help you locate specific information without scanning the entire content.
Create Clustered INDEX idx_UserName ON Users(UserName) => Users is the table and the index is on the UserName column.
Clustered: rows are organized based on the order of the index column; one clustered index per table.
Non-Clustered: no effect on order of rows, creates a sorted order of indexed columns; multiple non-clustered indexes per table

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

What are the basic parts of a simple TSQL Query?

A

Select statement
From clause
Where clause
Order By clause
Group by clause
Joins

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

What is a database “Table”?

A

A structured collection of ROWS and COLUMNS used to store data. Each row is a record and each column represents the specific field or attribute of that record.

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

What are the different types of statements available to you in TSQL?

A
  1. Data Definition Language (DDL): Create, Alter, Drop, Truncate
  2. Data Query Language (DQL): Select
  3. Data Manipulation Language (DML): Select, Insert, Update, Delete
  4. Data Control Language (DCL): Grant, Revoke
  5. Transaction Control Language (TCL): Commit, Rollback, Savepoints
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the different types of Joins? (Write examples of each)

A

Inner join: return only the rows where there is a match in both tables
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Left Join (or Left Outer Join): return all rows from left table and only the matched rows from right
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Right Join (or Right Outer Join): return all rows from the right table and only the matched rows from left
SELECT Employees.EmployeeName, Orders.OrderID
FROM Employees
RIGHT JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID;

Full Outer Join: returns all rows when there is a match in either the left or the right table. Null values will be returned in non-matching columns.
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Cross Join: Cartesian product: all possible combos of rows from both tables
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;

Self Join: Joins a table with itself
SELECT e1.EmployeeName AS Employee, e2.EmployeeName AS Supervisor
FROM Employees e1
INNER JOIN Employees e2 ON e1.SupervisorID = e2.EmployeeID;

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

What is a Database?

A

A structured collection of data that is organized for efficient storage, retrieval, and management. It typically consists of tables (to store the information), procedures (sets of instructions to perform specific tasks), and functions (which are reusable routines that can be called to perform specific operations.

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

What is a Foreign Key and how many can one table have?

A

A foreign key uses the Primary Key from another table where the information is the same. There is typically no limit to how many foreign keys one table can use, but depending on the server or the specific DBMS (Database Management System), there may be limits to consider.

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

What is a Primary Key and how many can one table have?

A

A column or a set of columns (composite key) that uniquely identifies each row in a table, specifically uniquely identifies the row within a specified column.

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

What is a relational Database?

A

A relational database is made up of tables of information that can share information with other tables within the same database by establishing primary and foreign key relationships.

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

What is a SQL Injection Attack and how do you protect yourself against these?

A

It is a type of security vulnerability that occurs when an attacker is able to manipulate a web application’s SQL query by injecting malicious SQL code.
To protect against this, it’s important to validate and clean user inputs, use secure coding practices, and limit the permissions of accounts to the bare minimum needed.

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

What is a Sql Server stored procedure?

A

It is a statement that can be re-utilized to add, access, or manipulate date stored in the database.

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

What is a SQL Server?

A

An SQL Server is a type of RDBMS (Relational Database Management System) for storing data that can be accessed using the SQL programming language (Structured Query Language).

17
Q

What is data normalization?

A

It is the process of organizing and structuring a relational database to reduce data redundancy and improve data integrity. This is accomplished by dividing large tables into smaller, related tables and defining relationships between them. This helps minimize duplicated data and reduces errors when performing operations such as adding, updating, or deleting.

18
Q

What is the truncate statement used for? What is the key difference between this and your other options to remove data?

A

“Truncate” statement clears out all ROWS of information from the specified table without any conditions. “Delete” statement to delete specific records based on conditions . “Drop” statement removes an entire object from the database (such as a table or stored procedure).

19
Q

What language do you use to communicate with the database?

A

We use SQL (Structured Query Language) to communicate with relational databases.

20
Q

What language is used to write Stored procedures?

A

T-SQL – T stands for Transactional

21
Q

When are “Joins” used?

A

We use joins when we want to pull information that is contained within more than one table.

22
Q

Why would we go through the process of normalizing our data?

A

We would use it to improve the efficiency, integrity, and maintainability of a database by reducing redundancies and minimizing the chances of errors during data manipulation.
1. Reduce Data Redundancy – minimize duplicated data by separating large tables into separate tables.
2. Avoid Update Anomalies – inconsistencies may occur when information is changed in one location, but not in another. This supports the need for relationships between tables.
3. Enhance Data Integrity – Using primary and foreign keys, we can maintain the accuracy and reliability of stored information.
4. Improve Query Performance – more structure within a database improves query performance and reduces unnecessary information retrieval
5. Simplify Maintenance – Changes typically can be made to the database structure without affecting other parts of the system