Interview Questions SQL - amitbhilagude/userfullinks GitHub Wiki
Difference between DELETE and TRUNCATE
DELETE is used to delete a specific row.
TRUNCATE is used to delete all rows. It is also possible to delete all rows using the DELETE command however it is slower than the TRUNCATE
Subsets of SQL
DDL: Data Definition Language used to create schema
DML: Data Manipulation Language used for manipulating data.
DCL: Data control language used for access control of data
DTL: Data transactional language used for transactional data.
Database Management System(DBMS) Types
Relational: Relationships in table
Hierarchical: nested tree structure
Network: Many to many relationships
Object-Oriented: Store each object
JOINS in SQL
Inner Join
Left Join
Right Join
Full JOin
Difference between CHAR and VARCHAR
CHAR is fixed length so if we define Char(10) then it is going to allow a string of length 10 only.
VARCHAR is variable-length so if we define varchar(10) then it will allow any length up to 10 char.
Primary Key and Foreign Key
Column in the table which has unique record and it is set as Primary Key to managing the relationship with other tables.
Link between Parent and Child Table where Parent Table has Primary Kay and Child Table has Foreign key. It will allow you query or avoid deleting records in Parent table.
Constraints: Allow you to limit the datatype
Not NULL: It Will make sure a particular column will never allow null value.
Unique: It will make sure a particular column will have a unique value
Check: It will allow the condition and column value must fall in that range
Default: Set the default value when no values are specified
Index: Used for indexing that column.
SQL vs SQL Server vs MySQL
SQL: Language
SQL Server: DBMS
MySQL: DMBS
Data Integrity.
Accuracy and Consistency of data is called Data Integrity.
Clustered Index vs Non-Cluster Index vs Unique Index
Clustered Index
Clustered Index alters actual record gets arrange with B+ Tree data structure
Data retrieval will be faster
One Cluster Index per table needs to be created
Non-Clustered Index
Actual Records doesn't get alter but the additional table will be maintained with pointers
Data retrieval will be slower
Allow many cluster index per table
Unique Index
Doesn't allow default value.
SQL Query to display the current date: Select GetDate()
What is Denormalisation?
It is a flat data structure and incorporates data into a single table.
It is opposite to the normalization
Entities and Relationship
Entity is a Unit that will have different attributes
Relationship is the relationship between the entities. It will be one-to-one, one-to-many, many-to-one and self referencing
Index
Performance tuning method to allow faster record retrieval
Advantages of Normalisation
Structure approach
Avoids Data Duplication and Redundancy
Faster query
Easy maintain
Difference between DROP and TRUNCATE
DROP will drop the table
Truncate will remove the rows.
Normalisation Forms
1NF: Each cell should have a unique value. Create separate tables and relationships to avoid this.
2NF: The database should be 1NF and it must have a single primary key.
3NF: The database should be 2NDF and it must not have any functional dependencies.
BCNF: after 3NF if you still more normalization due to anomaly to continue so it will be one candidate key.
ACID Property
Automicity : Commit all transactions or options to roll back
Consistency
Isolation
Durability
Operators
Arithmetic
Bitwise
Comparision
Compound
Logical
Cross Join vs Natural Join
Cross columns of two tables which we join
Natural Join of two tables both have the same columns and data type.
Different Types of Subquery
Correlated and Non-Correlated: Inner query is referring out query table then it is correlated else non-correlated.
Query with the name of employee begin with A is like "A%"
Group Function: It returns only one result e.g. AVG, COUNT, MAX, MIN, SUM
Difference between BETWEEN and IN
Between has range however IN check value in a set of values like IN(10,20,30)
MERGE: Conditional update. If the row not exists will do INSERT, If the row exists then will do an update.
Difference between HAVING and WHERE
HAVING will be applied for Select and always with Group By else it will work as WHERE
WHERE will be applied for each type of statement.
Ways to Execute Dynamic SQL
Write query with parameter
exe
sp_executesql
Case Manipulations: Lower, upper, Initcap
SET Operations: UNIONS, INTERSECT and MINUS
ALIAS Command: Give Particular name AS and user in where clause.
Aggregate and Scaler Functions
Aggregrate functions are arithmetic function e.g. sum or max
Scaler function is return value based on input e.g. Now()
How to fetch alternate record: Use of Mod function e.g Mode(rowno, 2)
Pattern Matching character % for 0 to more and _ for one character
Select Unique records using a distinct keyword.
SQL Vs PL/SQL
SQL: Single statement for insert, update, or delete
PL/SQL: Called Procedural SQL to need to define a variable and then use these statement of insert or update or delete.
Views
Virtual Table with a relationship where we can create the Views
Different types of User-Defined functions
Scaler functions
Inline table value function
Multi statement value functions
Collations
Rules on how data can be sorted or compare
Auto Increment
Commonly used for Primarykey to auto increment. It uses IDENTITY command