53 SQL Questions-Answers
53 SQL Questions-Answers
53 SQL Questions-Answers
Questions-Answers
Watch Full Video On Youtube:
https://youtu.be/pKFo8Mqp-cU
If the database name specified conflicts with an existing database MySQL will display and error message reporting this fact:
In this situation, a different database name should be selected, or the IF NOT EXISTS option should be used. This option only
creates the database if it does not already exist:
SQL QUERY:
CREATE TABLE department_copy
as (SELECT * FROM department WHERE 1=2);
3. What Is The SQL Query Used To Create A Table
With Same Structure With Data Of Another Table
SQL QUERY:
CREATE TABLE Emp SELECT * FROM Employee;
What Is
4. What Is The
TheSQL
SQLQuery
QueryUsed
UsedToToFind
FindThe
The 2nd
2nd / /
3rd
3rd // nth
nth Highest
HighestSalary?
Salary?
1. USING SUB-QUERY:
SELECT MAX(Salary) FROM Employee
WHERE Salary<(SELECT MAX(Salary) FROM Employee
WHERE Salary<(SELECT MAX(Salary) FROM Employee));
What Is
4. What Is The
TheSQL
SQLQuery
QueryUsed
UsedToToFind
FindThe
The 2nd
2nd / /
3rd
3rd // nth
nth Highest
HighestSalary?
Salary?
2. USING LIMIT:
SELECT Salary FROM Employee
ORDER BY Salary DESC LIMIT N-1,1;
What Is
4. What Is The
TheSQL
SQLQuery
QueryUsed
UsedToToFind
FindThe
The 2nd
2nd / /
3rd
3rd // nth
nth Highest
HighestSalary?
Salary?
3. USING LIMIT-OFFSET:
SELECT Salary FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET N-1;
For Example:
// 3rd Highest Salary//
4. USING DISTINCT:
SELECT Salary FROM Employee e1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(e2.Salary)) FROM Employee e2
WHERE e2.Salary > e1.Salary);
5. What Is The SQL Query Used To Find All Employees
Who Also Hold The Managerial Position?
SQL QUERY:
SELECT * FROM EMPLOYEES
WHERE (EMPLOYEE_ID IN (SELECT MANAGER_ID FROM EMPLOYEES));
6. What Is The SQL Query Used To Find The Names Of
The Employees That Begin With ‘A’?
SQL QUERY:
SELECT Name FROM Employee WHERE Name LIKE 'A%';
SELECT CURRENT_DATE;
SELECT CURRENT_DATE();
SELECT CURDATE();
SELECT DATE(NOW());
SELECT DATE(CURRENT_TIMESTAMP());
NOTE: The NOW() & CURRENT_TIMESTAMP() functions returns the current
date and time in "YYYY-MM-DD HH-MM-SS" (string) or as
YYYYMMDDHHMMSS (numeric) format. So we need to fetch date using date()
8(i). What Is The SQL Query Used To Fetch Alternate
Records From A Table?
SQL Query:-
SELECT * FROM Employee
INNER JOIN Emp ON Employee.ID = Emp.ID;
9(i). What Is The SQL Query Used To Find Duplicate
Rows In Table?
SQL Query:-
SELECT column, COUNT(column)
FROM table_name
GROUP BY column
HAVING COUNT(column) > 1;
SQL Query:-
DELETE E1 FROM EMPLOYEE E1
INNER JOIN EMPLOYEE E2
WHERE
E1.id < E2.id AND
E1.Name = E2.Name;
10. What Is The SQL Query Used To Find
The nth Record From A Table?
SQL Query:-
Syntax : SELECT * FROM <table_name> LIMIT N-1,1;
Example : SELECT * FROM Employee LIMIT 0,1;
SQL Query:-
SELECT * FROM Employee LIMIT 5;
SELECT * FROM Employee ORDER BY ID LIMIT 5;
11(ii). What Is The SQL Query Used To Find
The Last 5 Records From A Table?
SQL Query:-
(SELECT * FROM Employee
ORDER BY ID DESC LIMIT 5)
ORDER BY ID ASC;
3. USING SUB-QUERY:
SELECT dept_id FROM Employee A
WHERE A.id >=
ALL(SELECT B.id FROM Employee B
WHERE A.dept_id = B.dept_id)
ORDER BY dept_id ;
14. What Is The SQL Query Used To Find The
Maximum Salary Of Each Department?
SQL QUERY:
SELECT dept_id, MAX(salary) FROM Employee
GROUP BY dept_id;
SQL QUERY:
SELECT dept_id, COUNT(dept_id) FROM Employee
GROUP BY dept_id
ORDER BY COUNT(dept_id);
For Example:
ALTER TABLE Employee MODIFY manager_id BIGINT ;
17. What Are Tables And Fields In The Database?
Table:
❏ A Table is an organized collection of data stored in the form of rows and columns.
❏ It enables users to store and display records in the structure format.
❏ It is similar to worksheets in the spreadsheet application.
❏ Fields are the components to provide the structure for the table. 1 Nitin Testing 25000
❏ It stores the same category of data in the same data type.
❏ A table contains a fixed number of columns but can have any
2 Pranshul Marketing 28000
number of rows known as the record.
★ The columns in a table are called FIELDS while the rows can be 3 Utkarsh Sales 24000
referred to as RECORDS.
★ The columns can be categorized as vertical and rows as horizontal.
17. What Are Tables And Fields In The Database?
❏ A Table is an organized collection of data stored in the form of rows and columns. 1 Nitin Testing 25000
❏ It enables users to store and display records in the structure format.
❏ It is similar to worksheets in the spreadsheet application.
2 Pranshul Marketing 28000
Field:
3 Utkarsh Sales 24000
❏ Fields are the components to provide the structure for the table.
❏ It stores the same category of data in the same data type.
❏ A table contains a fixed number of columns but can have any number of rows known as the record.
★ The columns in a table are called FIELDS while the rows can be referred to as RECORDS.
★ The columns can be categorized as vertical and rows as horizontal.
Example:
Table: Employee.
Field: ID, Name, Department, Salary
Record: 1, Nitin, Development, 25000
18. What Is The Difference Between Unique Key,
Primary key And Foreign key?
❏ The role of the Unique key is to make sure that each column and row are 1 Nitin Testing 25000
unique.
❏ The Unique key cannot accept the duplicate values. 2 Pranshul Marketing 28000
❏ It can accept a null value but only one null value per column.
❏ It ensures the integrity of the column or group of columns to store different
3 Utkarsh Sales 24000
values into a table
Syntax: UNIQUE constraint on single column Syntax: UNIQUE constraint on multiple columns
CREATE TABLE Employee( CREATE TABLE Employee (
ID int NOT NULL, ID int NOT NULL,
Name varchar(255) NOT NULL, Name varchar(255) NOT NULL,
Department varchar(255), Department varchar(255),
Salary int, Salary int,
UNIQUE (ID)); CONSTRAINT UC_Employee UNIQUE (ID, Name));
18. What Is The Difference Between Primary Key,
Foreign key And Unique key?
❏ A Primary key is used to uniquely identify all table records. 1 Nitin Testing 25000
❏ You can consider Primary Key constraint to be a combination of UNIQUE and
NOT NULL constraint. 2 Pranshul Marketing 28000
i.e Primary Key = UNIQUE + NOT NULL
❏ This means that if a column is set as a primary key, then this particular column
3 Utkarsh Sales 24000
cannot have any null values present in it and also all the values present in this
column must be unique.
❏ A table can have only one primary key that consists of single or multiple fields.
Syntax: PRIMARY constraint on single column Syntax: PRIMARY constraint on multiple columns
CREATE TABLE Employee( CREATE TABLE Employee (
ID int NOT NULL, ID int NOT NULL,
Name varchar(255) NOT NULL, Name varchar(255) NOT NULL,
Department varchar(255), Department varchar(255),
Salary int, Salary int,
PRIMARY KEY (ID)); CONSTRAINT PK_Employee PRIMARY KEY (ID, Name));
18. What Is The Difference Between Primary Key,
Foreign key And Unique key?
There cannot be more than one Primary key but unique keys can be multiple.
1. The primary key act as a unique identifier for each The unique key is also a unique identifier for records
record in the table. when the primary key is not present in the table.
2. We cannot store NULL values in the primary key We can store NULL value in the unique key column, but
column only one NULL is allowed.
3. We cannot change or delete the primary key column We can modify the unique key column values.
values.
18. What Is The Difference Between Primary Key,
Foreign key And Unique key?
FOREIGN KEY:
❏ The foreign key is used to link one or more tables together. It is also known as the Referencing Key.
❏ A Foreign key is specified as a key that is related to the primary key of another table. It means a foreign key field in
one table refers to the primary key field of the other table.
❏ A Foreign key is a field that can uniquely identify each row in another table.
❏ The primary key-foreign key relationship is a very crucial relationship as it maintains the ACID properties of the
database sometimes.
Syntax:
CREATE TABLE Employee (
ID int NOT NULL,
Name varchar(255) NOT NULL,
Dept_ID int,
Salary int,
PRIMARY KEY (ID),
FOREIGN KEY (Dept_ID) REFERENCES Department(ID));
18. What Is The Difference Between Primary Key,
Foreign key And Unique key?
FOREIGN KEY:
Consider the two tables as shown:
As we can clearly see, that the field Dept_Id in the Employee table is the primary key in the Department table, i.e. it uniquely
identifies each row in the Department table. Therefore, it is a Foreign Key in the Employee table.
JOINS:
❏ JOINS are used to retrieve data from multiple tables into a meaningful result set.
❏ It is performed whenever you need to fetch records from two or more tables.
INNER JOIN:
❏ Inner Join basically returns records that have matching
values in both tables.
Syntax:
SELECT * FROM Table_A INNER JOIN Table_B;
20. What Is A Join? List Different Types Of Joins.
CROSS JOIN:
❏ Cross join can be defined as a cartesian product of the
two tables included in the join.
❏ It returns rows combining each row from the first table
with each row of the second table.
❏ For Example - If we join two tables having 10 and 15
columns the Cartesian product of two tables will be
10×15=150 rows.
Syntax:
SELECT a.name, d.name FROM EMPLOYEE AS a
CROSS JOIN DEPARTMENT as d;
20. What Is A Join? List Different Types Of Joins.
SELF JOIN:
❏ A SELF JOIN is used to join a table with itself.
❏ This join can be performed using table aliases, which allow us
to avoid repeating the same table name in a single sentence.
❏ It will throw an error if we use the same table name more than
once in a single query without using table aliases.
❏ A SELF JOIN is required when we want to combine data with
other data in the same table itself.
Syntax:
SELECT column_lists
FROM TABLE1 AS T1, TABLE1 AS T2
WHERE join_conditions;
‘DELETE’ Statement:
DELETE removes some or all rows from a
table based on the condition.
It can be rolled back.
Example:
DELETE FROM Emp
WHERE id > 10;
21. What Are ‘TRUNCATE’, ‘DELETE’ And ‘DROP’
Statements In SQL?
‘TRUNCATE’ Statement:
TRUNCATE command is used to delete all
the rows from the table and free the space
containing the table. Truncate operation
cannot be rolled back.
Example:
TRUNCATE TABLE emp;
21. What Are ‘TRUNCATE’, ‘DELETE’ And ‘DROP’
Statements In SQL?
‘DROP’ Statement:
DROP command is used to remove an
object from the database. If you drop a
table, all the rows in the table are deleted
and the table structure is removed from the
database. It also cannot be retrieved back.
Example:
DROP TABLE emp;
21. What Are ‘TRUNCATE’, ‘DELETE’ And ‘DROP’
Statements In SQL?
DELETE removes some or all rows from a table based on the condition. It can
be rolled back.
TRUNCATE command is used to delete all the rows from the table and free the
space containing the table. Truncate operation cannot be rolled back.
DROP command is used to remove an object from the database. If you drop a
table, all the rows in the table are deleted and the table structure is removed
from the database. It also cannot be retrieved back.
21. What Are ‘TRUNCATE’, ‘DELETE’ And ‘DROP’
Statements In SQL?
1. The Truncate command deletes the whole contents of an The Delete statement removes single or multiple rows from an
existing table without the table itself. It preserves the table existing table depending on the specified condition.
structure or schema.
3. We cannot use the WHERE clause with TRUNCATE. We can use the WHERE clause in the DELETE command.
4. TRUNCATE is used to remove all the rows from a table. DELETE is used to delete a row from a table.
5. TRUNCATE is faster than DELETE as it deletes entire data DELETE statement is slower because it maintained the log.
at a time without maintaining transaction logs.
6. It is not possible to roll back after using the TRUNCATE. You can roll back data after using the DELETE.
7. TRUNCATE query occupies less space. DELETE query takes more space.
Source: https://www.javatpoint.com/
22. What Is An ‘Alias’ In SQL?
ALIAS:
❏ This command provides another name to a table or a column.
❏ It can be used in WHERE clause of a SQL query using the
“AS” keyword.
❏ Aliases are created to make table or column names more
readable.
❏ The renaming is just a temporary change and the table name
does not change in the original database.
❏ Aliases are useful when table or column names are big or not
very readable.
Example:
SELECT e.name AS "Employee Name",
d.name AS "Department Name",
e.salary AS "Monthly Salary",
e.hiredate AS "Date Of Joining"
FROM Employee AS e
LEFT OUTER JOIN Department d
on e.dept_id = d.id;
23. What Are Constraints In SQL?
CONSTRAINTS:
❏ Constraints are used to specify the rules that we can apply to the type of data in a table. That is, we can specify the
limit on the type of data that can be stored in a particular column in a table using constraints.
❏ It can be applied for single or multiple fields in an SQL table during the creation of the table or after creating using the
ALTER TABLE command.
❏ It enforces us to store valid data and prevents us from storing irrelevant data.
7 It does not allow to work with aggregate functions. It can work with aggregate functions.
Example of IN:
SELECT * FROM employee
WHERE salary
IN(23000,25000,26000,30000);
25. What Is The Difference Between ‘IN’ And
‘BETWEEN’ Operators In SQL?
1. This operator is used to selects the range of data It is a logical operator to determine whether or not a
between two values. The values can be numbers, specific value exists within a set of values. This operator
text, and dates as well. reduces the use of multiple OR conditions with the query.
2. It returns records whose column value lies in between It compares the specified column's value and returns the
the defined range. records when the match exists in the set of values.
3. Syntax: Syntax:
ORDER BY:
❏ The ORDER BY clause is used to sort the table
data either in ascending or descending order.
❏ By default ORDER BY sorts the data in
ASCENDING ORDER.
❏ If we want to change its default behavior, we need
to use the DESC keyword after the column name in
the ORDER BY clause.
Syntax:
SELECT expressions FROM tables
ORDER BY expression [ASC | DESC];
Example:
SELECT name, salary FROM EMPLOYEE
ORDER BY SALARY ASC;
DISTINCT:
❏ The DISTINCT keyword is used to ensure that the
fetched value always has unique values.
❏ It does not allow to have duplicate values.
❏ The DISTINCT keyword is used with the SELECT
statement and retrieves different values from the
table's column.
Syntax:
SELECT DISTINCT column FROM table;
Example:
SELECT DISTINCT dept_id FROM Employee;
28. What Are Aggregate Functions. How many
Aggregate Functions Are Available In SQL?
AGGREGATE FUNCTIONS:
❏ Aggregate functions are used to evaluate mathematical calculation and
return single values.
❏ Aggregate functions are often used with the GROUP BY and HAVING
clauses of the SELECT statement.
❏ This can be calculated from the columns in a table.
Database Relationship:
Database Relationship is defined as the connection between the tables in a database.
Following are the relationships in SQL.
❏ One-to-One relationship - This can be defined as the relationship between two tables where each
record in one table is associated with the maximum of one record in the other table.
❏ One-to-Many & Many-to-One relationships - This is the most commonly used relationship where a
record in a table is associated with multiple records in the other table.
❏ Many-to-Many relationship- This is used in cases when multiple instances on both sides are needed
for defining a relationship.
❏ Self-Referencing Relationships - This is used when a table needs to define a relationship with itself.
31. What Is RDBMS? How Is It Different From DBMS?
RDBMS:
❏ RDBMS stands for Relational Database Management System that stores data in the form of a collection of tables,
and relations can be defined between the common fields of these tables.
❏ It also provides relational operators to manipulate the data stored into the tables.
❏ Examples of relational database management systems are Microsoft Access, MySQL, SQL Server, Oracle
database, etc.
31. What Is RDBMS? How Is It Different From DBMS?
1. DBMS applications store data as file. RDBMS applications store data in a tabular form.
2. In DBMS, data is generally stored in either a In RDBMS, the tables have an identifier called primary key and
hierarchical form or a navigational form. the data values are stored in the form of tables.
4. DBMS does not apply any security with regards RDBMS defines the integrity constraint for the purpose of ACID
to data manipulation. (Atomicity, Consistency, Isolation and Durability) property.
5. DBMS uses file system to store data, so there in RDBMS, data values are stored in the form of tables, so a
will be no relation between the tables. relationship between these data values will be stored in the
form of a table as well.
7. DBMS does not support distributed database. RDBMS supports distributed database.
8. Examples of DBMS are file systems, xml etc. Example of RDBMS are mysql, postgre, sql server, oracle etc.
Source: https://www.javatpoint.com/difference-between-dbms-and-rdbms
32. What Is A QUERY?
QUERY:
❏ An SQL query is used to retrieve the required data from the database.
❏ A query is a code written in order to get the information back from the database.
❏ However, there may be multiple SQL queries that yield the same results but with different levels of efficiency.
❏ An inefficient query can drain the database resources, reduce the database speed or result in a loss of
service for other users.
❏ So it is very important to optimize the query to obtain the best database performance.
❏ Syntax:
SELECT * FROM EMP;
33. What Is A SUB-QUERY? What Are Its Types?
SUB-QUERY:
❏ A Subquery can be simply defined as a query within another query.
❏ The outer query is called as MAIN QUERY, and inner query is called SUB-QUERY.
❏ SubQuery is always executed first, and the result of subquery is passed on to the main query.
❏ For example:
SELECT MAX(salary) FROM Employee
WHERE Salary<(SELECT MAX(Salary) FROM Employee
WHERE Salary<(SELECT MAX(Salary) FROM Employee));
33. What Is A SUB-QUERY? What Are Its Types?
SUB-QUERY:
❏ A Subquery can be simply defined as a query within another query.
❏ The outer query is called as MAIN QUERY, and inner query is called SUB-QUERY.
❏ SubQuery is always executed first, and the result of subquery is passed on to the main query.
❏ A subquery can also use any comparison operators such as >,< or =.
❏ For example:
SELECT MAX(salary) FROM Employee
WHERE Salary<(SELECT MAX(Salary) FROM Employee
WHERE Salary<(SELECT MAX(Salary) FROM Employee));
Types Of SUB-QUERY:
There are two types of subquery – Correlated and Non-Correlated.
❏ Correlated Subquery - A correlated subquery cannot be considered as independent query, but it
can refer the column in a table listed in the FROM the list of the main query.
❏ Non-Correlated Subquery - It can be considered as independent query and the output of
subquery are substituted in the main query.
34. What Is A Self-Join. What Is Its Requirement.
Syntax:
SELECT column_lists
FROM TABLE1 AS T1, TABLE1 AS T2
WHERE join_conditions;
Source: https://www.javatpoint.com/
35. What Is The Difference Between NOW() And
CURRENT_DATE()?
NOW():
NOW() Command will fetch the current date and
time both in format ‘YYYY-MM-DD HH:MM:SS’
SELECT NOW();
CURRENT_DATE():
CURRENT_DATE() Command will fetch the date of
the current day in format ‘YYYY-MM-DD’.
SELECT CURRENT_DATE();
36. Which Function Is Used To Return Remainder In
A Division Operator In SQL?
❏ DROP TABLE command deletes complete data from the table along
with removing the complete table structure too.
❏ In case our requirement was only just remove the data, then we would
need to recreate the table to store data in it.
❏ In such cases, it is advised to use the TRUNCATE command.
38. What Is The Difference Between CHAR And
VARCHAR2 Datatype In SQL?
SR.NO CHAR VARCHAR
1. CHAR datatype is used to store character string of fixed length VARCHAR datatype is used to store character string of variable length
In CHAR, If the length of string is less than set or fixed length then In VARCHAR, If the length of string is less than set or fixed length then it will
2.
it is padded with extra memory space. store as it is without padded with extra memory spaces.
Storage size of VARCHAR datatype is equal to the actual length of the entered
4. Storage size of CHAR datatypes is equal to n bytes i.e. set length
string in bytes.
We should use CHAR datatype when we expect the data values in We should use VARCHAR datatype when we expect the data values in a
5.
a column are of same length. column are of variable length.
VARCHAR take 1 byte for each character and some extra bytes for holding
6. CHAR take 1 byte for each character
length information
Source: https://www.geeksforgeeks.org/char-vs-varchar-in-sql/
39. What Are Scalar Functions?
SCALAR FUNCTIONS():
Scalar functions are the built-in functions in SQL, and whatever be the
input provided to the scalar functions, the output returned by these
functions will always be a single value.
Following are the widely used SQL scalar functions:
LENGTH() - Calculates the total length of the given field (column).
SELECT name, LENGTH(name) FROM Department;
UCASE() - Converts a collection of string values to uppercase characters.
SELECT UCASE(name) FROM Department;
LCASE() - Converts a collection of string values to lowercase characters.
SELECT LCASE(name) FROM Department;
MID() - Extracts substrings from a collection of string values in a table.
SELECT MID(name,2,4) FROM Department;
ROUND() - Calculates the round-off integer value for a numeric field (or
decimal point values).
SELECT Round(26.837352835283528,4) as Value;
NOW() - Returns the current date & time.
SELECT NOW() AS Today;
CONCAT() - Concatenates two or more strings.
SELECT CONCAT(id, name, location) FROM Department;
RAND() - Generates a random collection of numbers of a given length.
FORMAT() - Sets the format to display a collection of values.
40. What is A Database?
DATABASE:
❏ A database is an organized collection of data for easy access, storing, retrieval and managing of data.
❏ This is also known as structured form of data which can be accessed in many ways.
❏ Almost every organization uses the database for storing the data due to its easily accessible and high
operational ease.
❏ The database provides perfect access to data and lets us perform required tasks.
❏ It is also the collection of schemas, tables, queries, views, etc.
❏ Example: School Management Database, Bank Management Database.
SQL:
❏ SQL is a structured query language used in a database.
❏ SQL is the core of the relational database which is used for accessing and
managing database.
41. What Is SQL? What Is The Difference Between
NoSQL vs SQL Databases?
SQL databases are a type of system software that supports NoSQL databases are a type of software that allows to
Define management, analysis, capturing and querying the structured maintain and retrieve structured, unstructured, polymorphic
data in a relational format. data for different purposes.
A language used to communicate with databases for storage, A software to retrieve, store and manage scalability of
Purpose
deletion, updation, insertion and retrieval of data. databases.
Development SQL was developed in the year 1970 for flat file storage NoSQL was developed in 2000 as an enhanced version for
Year problems. SQL databases for unstructured and semi-structured data.
Query
SQL databases support Structured Query Languages. NonSQL does not have any declarative query language.
Language
Source: https://www.interviewbit.com/blog/sql-vs-nosql/
41. What Is SQL? What Is The Difference Between
NoSQL vs SQL Databases?
SQL supports predefined schemas, making the storage of data Nosql supports dynamic schemas to store different forms of
Schemas
restrictive to structured type only. data.
Databases that support SQL require powerful hardware to support NonSQL databases require commodity hardware for
Hardware
vertical scaling. horizontal scaling.
SQL enables ACID(atomicity, consistency, isolation, and NonSQL follows CAP (consistency, availability, partition
Properties
durability) properties tolerance) properties.
Source: https://www.interviewbit.com/blog/sql-vs-nosql/
41. What Is SQL? What Is The Difference Between
NoSQL vs SQL Databases?
Data Storage SQL does not support hierarchical storage of data. NoSQL is best suited for hierarchical storage of data.
Distributed SQL databases can only be run on a single system and hence, NoSQL databases are designed to follow data distribution
Data does not follow distribution of data. features like repetition, partition.
Top
Companies Microsoft, Dell, Cognizant, etc. Amazon, Capgemini, Adobe, etc.
Using
Examples SQL supports databases like MySQL, SQL Server, Oracle, etc. Nosql databases are Hbase, MongoDB, Redis, etc.
Source: https://www.interviewbit.com/blog/sql-vs-nosql/
42. What Is The Difference Between SQL And MySQL?
2. It is used for query and operating database system It allows data handling, storing, and modifying data in an
organized manner.
4. Only a single storage engine is supported in SQL. MySQL supports multiple storage engines.
5. The server is independent in SQL During backup sessions, the server blocks the database.
6. SQL is a standard language which stands for Structured MySQL is a database management system.
Query Language based on the English language
7. SQL is the core of the relational database which is used MySQL is an RDMS (Relational Database Management
for accessing and managing database System) such as SQL Server, Informix etc.
8. SQL is a programming language, so that it does not get MySQL is software, so it gets frequent updation.
any updates. Its commands are always fixed and remain
the same.
43. What Do You Mean By DBMS? What Are Its
Different Types?
DBMS:
❏ A Database Management System (DBMS) is a software application that interacts with the user, applications, and
the database itself to capture and analyze data. (A database is a structured collection of data.)
❏ DBMS is a system software responsible for the creation, retrieval, updation, and management of the database.
❏ A DBMS allows a user to interact with the database. The data stored in the database can be modified, retrieved
and deleted and can be of any type like strings, numbers, images, etc.
❏ Without the database management system, it would be far more difficult for the user to access the database's
data.
No. The NULL value is not the same as zero or a blank space.
The following points explain their main differences:
❏ In SQL, zero or blank space can be compared with another zero or blank space. whereas
one null may not be equal to another null. null means data might not be provided or there
is no data.
45. What Is Union, Union ALL, Minus And Intersect?
UNION:
The UNION operator combines the results of two or more Select statements by removing duplicate rows. The columns and
the data types must be the same in the SELECT statements.
SELECT columns FROM table1 UNION SELECT columns FROM table2;
UNION ALL:
This operator is similar to the Union operator, but it does not remove the duplicate rows from the output of the SELECT
statements.
SELECT columns FROM table1 UNION ALL SELECT columns FROM table2;
INTERSECT:
This operator returns the common records from two or more SELECT statements.
SELECT columns FROM table1 INTERSECT SELECT columns FROM table2;
MINUS:
This operator returns the records from the first query, which is not found in the second query. It does not return duplicate
values.
SELECT columns FROM table1 MINUS SELECT columns FROM table2;
Before running either of the above SQL statements, certain requirements must be satisfied –
❏ Within the clause, each SELECT query must have the same amount of columns.
❏ The data types in the columns must also be comparable.
❏ In each SELECT statement, the columns must be in the same order
46. What Are The ACID Properties In A Database?
ACID PROPERTIES:
❏ The ACID properties are meant for the transaction that goes through a different group of tasks.
❏ A transaction is a single logical order of data.
❏ It provides properties to maintain consistency before and after the transaction in a database.
❏ It also ensures that the data transactions are processed reliably in a database system.
The ACID property is an acronym for Atomicity, Consistency, Isolation, and Durability.
Atomicity: It ensures that all statements or operations within the transaction unit must be executed successfully. If one part
of the transaction fails, the entire transaction fails, and the database state is left unchanged. Its main features are COMMIT,
ROLLBACK, and AUTO-COMMIT.
Consistency: This property ensures that the data must meet all validation rules. In simple words, we can say that the
database changes state only when a transaction will be committed successfully. It also protects data from crashes.
Isolation: This property guarantees that the concurrent property of execution in the transaction unit must be operated
independently. It also ensures that statements are transparent to each other. The main goal of providing isolation is to
control concurrency in a database.
Durability: This property guarantees that once a transaction has been committed, it persists permanently even if the
system crashes, power loss, or failed.
Source: https://www.javatpoint.com/
47. What Is Normalization? What Are The Various
Forms Of Normalization?
NORMALIZATION:
❏ Generally, in a table, we will have a lot of redundant information which is not required, so it is better to divide this
complex table into multiple smaller tables which contains only unique information.
❏ The process of table design to minimize the data redundancy is called normalization.
❏ In other words, Normalization in SQL is the process of organizing data to avoid duplication and redundancy.
❏ It includes the creation of tables, establishing relationships between them, and defining rules for those relationships.
❏ The main aim of Normalization is to add, delete or modify field that can be made in a single table.
❏ If any change is made in the data of one table but not made in the same data of another
table, then inconsistency will occur.
❏ This inconsistency will lead to the maintenance problem and effects the ACID properties
as well.
49. What Is Denormalization?
DENORMALIZATION:
❏ Denormalization is the inverse process of normalization, where the normalized schema is converted into a schema
that has redundant information.
❏ Denormalization is a technique used to access the data from higher to lower normal forms of database.
❏ The performance is improved by using redundancy and keeping the redundant data consistent.
❏ The reason for performing denormalization is the overheads produced in the query processor by an over-
normalized structure.
❏ Denormalization doesn't mean that normalization will not be done. It is an optimization strategy that takes place
after the normalization process.
❏ It adds redundant terms into the tables to avoid complex joins and many other complex operations.
50. What Is An Index? Explain Different Types Of
Indexes In SQL?
DATABASE INDEX:
❏ A database index is a data structure that provides a quick lookup of data in a column or columns of a table.
❏ An index creates an entry for each value and hence it will be faster to retrieve data.
❏ It also has a unique value meaning that the index cannot be duplicated.
❏ It is used to increase the performance and allow faster retrieval of records from the table.
❏ Indexes help speed up searching in the database.
❏ If there is no index on any column in the WHERE clause, then SQL Server has to skim through the entire table and
check each and every row to find matches, which might result in slow operation on large data.
❏ Syntax:
CREATE INDEX INDEX_NAME ON TABLE_NAME (COLUMN)
1. A clustered index is a table or view where the data The indexes other than PRIMARY indexes (clustered
for the rows are stored. In a relational database, if indexes) are called non-clustered indexes. It has a structure
the table column contains a primary key, MySQL separate from the data row. The non-clustered indexes are
automatically creates a clustered index named also known as secondary indexes.
PRIMARY.
2. Clustered indexes store the data information and Non-clustered indexes stores only the information, and then
the data itself. it will refer you to the data stored in clustered data.
3. There can only be one clustered index per table. There can be one or more non-clustered indexes in a table.
4. A clustered index determines how data is stored It creates a logical ordering of data rows and uses pointers
physically in the table. Therefore, reading from a for accessing the physical data files. Therefore, reading from
clustered index is faster. a clustered index is slower.
5. A clustered index always contains an index id of 0. A non-clustered index always contains an index id>0.
Source: https://www.javatpoint.com/
52. What Is A View In Database?
DATABASE VIEW:
❏ A view in SQL is a virtual table based on the result-set of SQL statement.
❏ A view contains rows and columns, just like a real table.
❏ The fields in a view are fields from one or more real tables in the database.
❏ View can have data of one or more tables combined, and it is depending on the relationship.
❏ If any changes occur in the existing table, the same changes reflected in the views also.
52. What Is A View In Database?
7. It only need backup from time to time as compared to OLTP Backup and recovery process is maintained religiously
8. This data is generally managed by CEO, MD, GM. This data is managed by clerks, managers.
9. Only read and rarely write operation. Both read and write operations.
Source: https://www.geeksforgeeks.org/difference-between-olap-and-oltp-in-dbms/
Thanks! Hope It Helps You!
Thanks
PS: Don’t Forget To Connect WIth ME.
Regards,
Nitin Mangotra (NitMan)
Connect with me:
Youtube: https://www.youtube.com/c/nitmantalks
Instagram: https://www.instagram.com/nitinmangotra/
LinkedIn: https://www.linkedin.com/in/nitin-mangotra-9a075a149/
Facebook: https://www.facebook.com/NitManTalks/
Twitter: https://twitter.com/nitinmangotra07/
Telegram: https://t.me/nitmantalks/