SQL Interview Questions Flashcards
source: https://www.edureka.co/blog/interview-questions/sql-interview-questions
ACID
ACID (Atomicity, Consistency, Isolation, Durability) - It is used to ensure that the data transactions are processed reliably in a database system.
- Atomicity - refers to the transactions that are completely done or failed where transaction refers to a single logical operation of a data. It means if one part of any transaction fails, the entire transaction fails and the database state is left unchanged.
- Consistency - ensures that the data must meet all the validation rules. In simple words, you can say that your transaction never leaves the database without completing its state.
- Isolation - The main goal of isolation is concurrency control.
- Durability - means that if a transaction has been committed, it will occur whatever may come in between such as power loss, crash or any sort of error.
Aggregate vs Scalar functions
Aggregate functions are used to evaluate mathematical calculation and returns a single value. These calculations are done from the columns in a table. For example- MAX(), COUNT(), SUM().
Scalar functions return a single value based on the input value. For example – UPPER(), NOW().
ALIAS
- ALIAS name can be given to any table or a column.
- This alias name can be referred in WHERE clause to identify a particular table or a column.
Authentication modes. How can it be changed?
Windows mode and Mixed Mode
- Click Start> Programs> Microsoft SQL Server and click SQL Enterprise Manager to run SQL Enterprise Manager from the Microsoft SQL Server program group.
- Then select the server from the Tools menu.
- Select SQL Server Configuration Properties, and choose the Security page.
Auto Increment
- automatically generate unique numbers when inserting new record to table.
- usually use auto increment column as primary key
- automatically increase value by 1 for each record, use parameters to change starting and increment values
- AUTO INCREMENT keyword can be used in Oracle and IDENTITY keyword can be used in SQL SERVER.
BETWEEN vs IN
- BETWEEN is a range
- IN check if value exist in given set
case manipulation functions
* LOWER: takes a string as an argument and returns it by converting it into lower case. LOWER(‘string’)
* UPPER: takes a string as an argument and returns it by converting it into uppercase. UPPER(‘string’)
* INITCAP: returns the string with the first letter in uppercase and rest of the letters in lowercase. INITCAP(‘string’)
CHAR vs VARCHAR2
- both character datatypes
- CHAR uses a fixed length (“abc” -> CHAR(10) -> “abc “ (10 character)
- VARCHAR2 can store any length up to the limit (“abc” -> VARCHAR(10) -> “abc” (3 character)
CLAUSE in SQL
- helps to filter/limit the rows from the entire set of records.
- WHERE, HAVING clause.
Clustered vs Non-clustered index
Clustered Index: * Physically stored the rows on the dick in the same order as the index * there can only be 1 cluster * faster to read from clustered index Non-Clustered Index: * a list that points to physical rows * can have many * takes time to write therefore slower than clustered index
Collation
Set of rules that determine how data can be sorted as well as compared.
Character data is sorted using the rules that define the correct character sequence along with options for specifying case-sensitivity, character width etc.
types of Collation Sensitivity
- Case Sensitivity: A and a and B and b.
- Kana Sensitivity: Japanese Kana characters.
- Width Sensitivity: Single byte character and double-byte character.
- Accent Sensitivity.
How can you fetch common records from two tables?
- fetch common records from two tables using INTERSECT
- example:
SELECT studentID FROM student
INTERSECT
SELECT StudentID FROM Exam
What are the various levels of constraints?
- column level constraint
- table level constraint
Constraints
- use to specify limits/rules when creating or altering tables
- list of constraints:
- NOT NULL
- CHECK
- DEFAULT
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
CROSS JOIN vs NATURAL JOIN
- CROSS JOIN - returns cross product of two tables
- JOIN - returns data from both table based on common column
Current date
* SELECT CURRENT_DATE;
Data Integrity
* refers to accuracy and consistency of data. * constraints helps with integrity when entered * business rules reinforcement
Datawarehouse
- Central repository of data where the data is assembled from multiple sources of information.
- Data are consolidated, transformed and made available for the mining as well as online processing.
- Warehouse data also have a subset of data called Data Marts.
What is DBMS? What are its different types?
- Database Management System (DBMS) is a software allowing interaction with the data. (PostGresSql, MS SQL, Oracle, etc)
- 2 types of DBMS:
- Relational Database Management System (RDBMS) - structured relational tables (MySQL, SQL etc)
- Non-Relational Database Management System - unstructured database (Mongo)
DELETE vs TRUNCATE
DELETE:
- delete a row in a table
- can rollback data after deletion
- uses DML command (Data Manipulation Language)
- slower than truncate
TRUNCATE:
- delete all rows in a table
- cannot rollback
- uses DDL command (Data Definition Language)
- faster than delete
Denormalization
- Opposite of normalization where it combines smaller tables into larger one.
- it causes redundancy
select unique records from a table
You can select unique records from a table by using the DISTINCT keyword
Example:
SELECT DISTINCT studentID
FROM Student
DROP vs TRUNCATE
DROP
- * Deletes the table
- * cannot be rollback
TRUNCATE
- * Removes all the rows from the table
List the ways in which Dynamic SQL can be executed?
- Write a query with parameters.
- Using EXEC.
- Using sp_executesql.
Entities and Relationships
- Entities are tables or data stored about a people, places or things (customer table, bank transaction)
- Relationships are links between entities (customer table and bank transaction are linked by customer id field)