SQL Flashcards
What is normalization
Normalization is the process of organizing data in a database.
This includes creating tables and establishing relationships between those tables
according to rules designed both to protect the data and to make the database more flexible by preventing redundancy and conflicting dependency.
What is primary Key
Is a constraint that uniquely identifies each row in a table.
It must contain UNIQUE values and implicitly NOT NULL
What is Foreign Key
is a single or collection of fields in a table refers to the PRIMARY KEY in another table
Normalization Rule
First Normal Form (1NF)
- Have a primary key
- All the columns in a table should have unique names.
- All the columns in a table should have one information
Second Normal Form (2NF)
- It should be in the First Normal form.
- And, it doesn’t have Partial Dependency
Third Normal Form (3NF)
- It is in the Second Normal form.
- And, it doesn’t have Transitive Dependency.
What are Constraints in SQL
Constraints are used to specify the rules concerning data in the table
It can be applied for single or multiple fields
NOT NULL, CHECK, DEFAULT, UNIQUE, INDEX , PRIMARY KEY, FOREIGN KEY
What is distinct
The SELECT DISTINCT statement is used to return only distinct (different) values
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
SELECT DISTINCT
column_name
FROM
table_name;
What is difference between unique and distinct in SQL?
The main difference between Unique and Distinct
Unique helps to ensure that all the values in a column are different while
Distinct helps to remove all the duplicate records when retrieving the records from a table.
What is a UNIQUE constraint?
A UNIQUE constraint ensures that all values in a column are different.
What is join
join is used to combine records (rows) from two or more tables based on a related column between the them
Different types of Join
There are four different types of JOINs in SQL
(INNER) JOIN:
Retrieves records that have matching values in both tables
LEFT (OUTER) JOIN:
Retrieves all the records/rows from the left and the matched records/rows from the right table
RIGHT (OUTER) JOIN:
Retrieves all the records/rows from the right and the matched records/rows from the left table.
FULL (OUTER) JOIN:
Retrieves all the records where there is a match in either the left or right table.
What is a View?
A view in SQL is a virtual table based on the result-set of an SQL statement.
The fields in a view are fields from one or more real tables in the database.
You can use views to hide table columns from users by allow them access to the view and not to the table itself.
This helps enhance database security and integrity.
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;
What are Aggregate function
An aggregate function performs operations on a collection of values to return a single scalar value.
Aggregate functions are often used with the GROUP BY and HAVING clauses of the SELECT statement
AVG() - Calculates the mean of a collection of values.
COUNT() - Counts the total number of records in a specific table or view.
MIN() - Calculates the minimum of a collection of values.
MAX() - Calculates the maximum of a collection of values.
SUM() - Calculates the sum of a collection of values.
FIRST() - Fetches the first element in a collection of values.
LAST() - Fetches the last element in a collection of values.
What is a Cross-Join?
The CROSS JOIN is a join for each row of one table to every row of another table.
at end the number of rows in the result-set is the product of the number of rows of the two tables
This join type is also known as cartesian join. … The main idea of the CROSS JOIN is that it returns the Cartesian product of the joined tables.
SELECT stu.name, sub.subject
FROM students AS stu
CROSS JOIN subjects AS sub;
Unlike other JOIN operators, it does not let you specify a join clause.
What is Denormalization?
Denormalization is the inverse process of normalization,
What are the TRUNCATE, DELETE and DROP statements?
DELETE :statement is used to delete rows one by one from a table.
(DML command)
Allow where clause
Can be rolled back
——————————————————————
TRUNCATE : command is used to delete all the rows from the table and free the space containing the table but not delete the schema
TRUNCATE is faster than delete
(DDL Command)
Not allow where clause
can be rolled back
———————————————————————–
DROP :command is used to remove a table, all the rows in the table are deleted and the table structure is removed from the database.
(DDL Command)
Not allow where clause
can not be rolled back