Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

SQL Interview

Uploaded by

Athira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

SQL Interview

Uploaded by

Athira
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

by

Database
Database is collection of data in a format that can be easily accessed (Digital)

A software application used to manage our DB is called DBMS (Database Management


System)
Types of Databases
Relational Non-relational (NoSQL)

Data stored in tables data not stored in tables

** We use SQL to work with relational DBMS


What is SQL?
Structured Query Language

SQL is a programming language used to interact with relational databases.

It is used to perform CRUD operations :


Create
Read
Update
Delete
Database Structure

Database

Table 1 Table 2

Data Data
What is a table?

Student table
SQL Datatypes
They define the type of values that can be stored in a column
Database related Queries

CREATE DATABASE db_name;

CREATE DATABASE IF NOT EXISTS db_name;

DROP DATABASE db_name;

DROP DATABASE IF EXISTS db_name;

SHOW DATABASES;

SHOW TABLES;
Types of SQL Commands

DDL (Data Definition Language) : create, alter, rename, truncate & drop

DQL (Data Query Language) : select

DML (Data Manipulation Language) : select, insert, update & delete

DCL (Data Control Language) : grant & revoke permission to users

TCL (Transaction Control Language) : start transaction, commit, rollback etc.


DDL(Data Defination Language)
DDL is a subset of SQL responsible for defining and managing the structure of database and
their objects.

1.create : used to create new table in the database.

CREATE TABLE table_name ( column_name1 datatype constraint, column_name2 datatype


constraint, );

Example :
Keys
Primary Key
It is a column (or set of columns) in a table that uniquely identifies each row. (a unique id)
There is only 1 PK & it should be NOT null.

Foreign Key

A foreign key is a column (or set of columns) in a table that refers to the primary key in another table.
There can be multiple FKs.

FKs can have duplicate & null values.


Keys
table1 - Student table2 - City
Constraints
SQL constraints are used to specify rules for data in a table.

NOT NULL columns cannot have a null value

UNIQUE all values in column are different

PRIMARY KEY makes a column unique & not null but used only for one
Constraints
FOREIGN KEY prevent actions that would destroy links between tables

DEFAULT sets the default value of a column


Constraints
CHECK it can limit the values allowed in a column
2. Alter :

used to modify the structure of the existing table.

you can add, modify, drop, columns, constraints and more.

Ex:

alter table students rename to stud;

3. Drop :

used to delete an existing table along with it’s data and structure.

Ex:

Drop table stud;


4. TRUNCATE :

used to remove all rows form a table . while reamining the table structure.

Ex:

truncate table employees;

5. Rename :

used to rename database object such as tables.

Ex:

alter table employees rename to staff;


ADD Column DROP Column

MODIFY Column RENAME Table

CHANGE Column (rename)


DQL(Data Query Language:

DQL is a subset of SQL focuses on retrieving data from databases.

1. Select :

the select statement is used to select data from database.

Syntax:
select colunm1, column2, ...;

if you want to select all the fields available into table , use the following syntax:

select * from table_name ;


DML (Data Manipulation Language) :

used to manipulate the data stored in database table.

DML commands enable you to insert , update, delete and retrieve data.

1. Insert : adds new row to the table .

Syntax:
INSERT INTO table_name (colname1, colname2)

VALUES (col1_v1, col2_v1), (col1_v2, col2_v2);


Create this sample table Insert this data
2. UPDATE:

Modify existing rows in a table.

Ex:

3. DELETE:

removes specific rows from a table based on a condition.

Ex:
Where Clause
To define some conditions

SELECT col1, col2 FROM table_name


WHERE conditions;
Where Clause
Using Operators in WHERE

Arithmetic Operators : +(addition) , -(subtraction), *(multiplication), /(division), %(modulus)

Comparison Operators : = (equal to), != (not equal to), > , >=, <, <=

Logical Operators : AND, OR , NOT, IN, BETWEEN, ALL, LIKE, ANY

Bitwise Operators : & (Bitwise AND), | (Bitwise OR)


Operators

AND (to check for both conditions to be true)

OR (to check for one of the conditions to be true)


Operators

Between (selects for a given range)

In (matches any value in the list)

NOT (to negate the given condition)


Operators
DISTINCT (Removes duplicate rows from query results)

Syntax: SELECT DISTINCT column1, column2 FROM table_name;

LIKE (The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column) T
There are two wildcards
There are twooften used
wildcards in conjunction
often withwith
used in conjunction thethe LIKE
LIKEoperator:
operator:
- The percent sign
- The (%) represents
percent zero, one,
sign (%) represents orone,
zero, multiple characters
or multiple characters
- The underscore sign
- The (_) represents
underscore one, single
sign (_) represents characterhere
one, single character are two wildcards often used in
conjunction with the LIKE operator:
- The percent sigThere are two wildcards often used in conjunction with the LIKE operator:
- The percent sign (%) represents
Syntax: zero,column1,
SELECT DISTINCT one, orcolumn2
multiple characters
FROM table_name;
- The undeThere are two wildcards often used in conjunction with the LIKE operator:
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single characterrscore sign (_) represents one, single
charactern (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character
EXAMPLE:-
Example: SELECT * FROM employees WHERE first_name LIKE 'J%';

WHERE CustomerName LIKE 'a%'


- Finds any values that start with "a"

WHERE CustomerName LIKE '%a'


- Finds any values that end with "a"

WHERE CustomerName LIKE '%or%'


- Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%'


- Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%'


- Finds any values that start with "a" and are at least 2 characters in length

WHERE CustomerName LIKE 'a__%'


- Finds any values that start with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o'


- Finds any values that start with "a" and ends with "o"
Limit Clause

Sets an upper limit on number of (tuples)rows to be returned

SELECT col1, col2 FROM table_name


LIMIT number;
Order By Clause

To sort in ascending (ASC) or descending order (DESC)

SELECT col1, col2 FROM table_name


ORDER BY col_name(s) ASC;
Aggregate Functions
Aggregare functions perform a calculation on a set of values, and return a single value.

COUNT( )
MAX( )
Get Maximum Marks
MIN( )
SUM( )
AVG( )

Get Average marks


Group By Clause
Groups rows that have the same values into summary rows.
It collects data from multiple records and groups the result by one or more column.

*Generally we use group by with some aggregation function.

Count number of students in each city


Having Clause
Similar to Where i.e. applies some condition on rows.
Used when we want to apply any condition after grouping.

Count number of students in each city where max marks cross 90.
General Order

SELECT column(s)
FROM table_name
WHERE condition
GROUP BY column(s)
HAVING condition
ORDER BY column(s) ASC;
Having Clause
Similar to Where i.e. applies some condition on rows.
Used when we want to apply any condition after grouping.

Count number of students in each city where max marks cross 90.
Cascading for FK
On Delete Cascade
When we create a foreign key using this option, it deletes the referencing rows in the child table
when the referenced row is deleted in the parent table which has a primary key.

On Update Cascade
When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the child
table when the referenced row is updated in the parent table which has a primary key.
Joins in SQL
Join is used to combine rows from two or more tables, based on a related column between them.
Types of Joins

Inner Join Left Join Right Join Full Join

Outer Joins
Inner Join
Returns records that have matching values in both tables

Syntax
SELECT column(s)
FROM tableA
INNER JOIN tableB
ON tableA.col_name = tableB.col_name;
SELECT *
Inner Join FROM student
Example INNER JOIN course
ON student.student_id = course.student_id;
student course

Result
Left Join
Returns all records from the left table, and the matched records from
the right table

Syntax
SELECT column(s)
FROM tableA
LEFT JOIN tableB
ON tableA.col_name = tableB.col_name;
SELECT *
Left Join FROM student as s
Example LEFT JOIN course as c
ON s.student_id = c.student_id;
student course

Result
Right Join
Returns all records from the right table, and the matched records
from the left table

Syntax
SELECT column(s)
FROM tableA
RIGHT JOIN tableB
ON tableA.col_name = tableB.col_name;
SELECT *
Right Join FROM student as s
Example RIGHT JOIN course as c
ON s.student_id = c.student_id;
student course

Result
Full Join
Returns all records when there is a match in either left or right table

Syntax in MySQL
LEFT JOIN
UNION
RIGHT JOIN
Full Join
Example
course
student

Result
Think & Ans
Qs: Write SQL commands to display the right exclusive join :

Left Exclusive Join Right Exclusive Join


Self Join
It is a regular join but the table is joined with itself.

Syntax

SELECT column(s)
FROM table as a
JOIN table as b
ON a.col_name = b.col_name;
Self Join
Example

Employee

Result
Union
It is used to combine the result-set of two or more SELECT statements.
Gives UNIQUE records.

To use it :
every SELECT should have same no. of columns
columns must have similar data types
columns in every SELECT should be in same order

Syntax
SELECT column(s) FROM tableA
UNION
SELECT column(s) FROM tableB
SQL Sub Queries
A Subquery or Inner query or a Nested query is a query within another SQL query.

It involves 2 select statements.


Query

Syntax Sub Query

SELECT column(s)
FROM table_name
WHERE col_name operator
( subquery );
SQL Sub Queries
Example
Get names of all students who scored more than class average.

Step 1. Find the avg of class


Step 2. Find the names of students with marks > avg
SQL Sub Queries
Example
Find the names of all students with even roll numbers.

Step 1. Find the even roll numbers


Step 2. Find the names of students with even roll no
SQL Sub Queries
Example with FROM
Find the max marks from the students of Delhi

Step 1. Find the students of Mumbai


Step 2. Find their max marks using the sublist in step 1
MySQL Views
A view is a virtual table based on the result-set of an SQL statement.

*A view always shows up-to-date data. The


database engine recreates the view, every time a
user queries it.
TCL ( Transaction Control Language):
TCL deals with the management of transactions with in a database.

1. commit :
the commit command is used to permanently save the changes made during a transaction.

once commit is executed , the transaction is considered successful , and changes are made
permanent.

Ex: commiting changes made during a transaction

update Employees
set salary = salary * 1.10
where dept = ‘sales’;

commit;
2. Rollback :-

the rollback command is used to undo changes made during the transaction.

it reverts all the changes applied to the database since the transaction began.

Ex:

BEGIN;

UPDATE Inventory
set quantity = quantity - 10
where product_id = 101;

--- an error occurs here ----

Rollback;
3. SAVEPOINT:

the savepoint command creates a named point with in a transaction , allowing you to set a
point to which you can later rollback needed.

Ex:

BEGIN;
UPDATE accounts
set balance = balance - 100
where acc_id = 131;
savepoint before withdrawal;

Update accounts
set balance = balance + 100
where acc_id = 147;

--- an error occur here ---

Rollback to before with withdrawal

---the first update is still applied ---

commit;
DCL (Data Control Language :
the DCL focuses on the management of access rights, permissions and security related aspects
of a database system.

DCL commands are used to control who can access the data, modify the data, or platform
administrative tasks with in a database.

1. GRANT:-

the grant command provide specific previledges or permissions to users or roles.

Syntax & Ex :
GRANT previledge_type GRANT select
ON Object_name ON Employees
to user_or_role; to Analyst;
2.REVOKE:

the REVOKE command is used to remove or revoke specific prviledges or permissions that
have been previously granted to users or roles.

Syntax & Ex:

REVOKE previledge_type
ON Object_name REVOKE select
to user_or_role; ON employees
from Analyst;
Realtime interview asked interview
questions (Infosys)
1].query to fetch employees whose salary is greater than 50,000.

2].Write a SQL query to find the second highest salary from the Employees table.

3].How do you count the number of records in a table?

4].Retrieve all records where the status is 'Active' from the Users table.

5].Explain the difference between an INNER JOIN and a LEFT JOIN.

6].Write a query to find all customers who have made more than one purchase.

7].How do you retrieve data from three tables using joins?

8].query to remove duplicate records from a table.


Explain the Normalization Concept in
SQL

Normalization is a Database Design Technique that organizes tables in a manner that


reduces redundancy & dependancy of data.

Normalization devided larger tables into smaller tables and links them using
relationships(primary key, foreign key).
Types Of Normal Forms

1. First Normal Form (1 NF)


2.Second Normal From(2NF)
3.Third Normal Form(3NF)
4.Boyce Codd Normal Form(BCNF)
5.Fourth Normal Form
1NF :

As per rule of 1NF

An attribute(column) of a table cannot hold multiple values.

it should be automic (Single unit )values.

Each record Need to be unique.

Note : Automic value is a value that cannot be devided.


Employee Table
Emp_id Name Mobile Dept
E1001 John 1234 IT
E1002 Smith 6789,7890 Sales
E1003 Kim 6785 HR

After 1 NF

Emp_id Name Mobile Dept


E1001 John 1234 IT
E1002 Smith 6789 Sales
E1002 Smith 7890 Sales
E1003 Kim 6785 HR
Explaination:
Each mobile no stored in a seperate row to ensure automicity .

Replace unclear entries with appropriate names .

Each Now has a unique combination.

Emp_id & Mobile No Ensuring No duplicate values.


2NF:
i
It should be 1NF

All Non - key attributes are fully functional dependent on the primary key.

it simple words should not have partial dependency.


Employee Table (1NF)
Emp_id Name Mobile Dept
E1001 John 1234 IT
E1002 Smith 6789 Sales
E1002 Smith 7890 Sales
E1003 Kim 6785 HR

After 2 NF

Emp_id Name Dept


E1001 John IT

T1 E1002 Smith Sales


E1003 Kim HR

Emp_id Mobile
E1001 1234
T2 E1002 6789
E1002 7890
E1003 6785
Explaination:
Ø Seperated table into 2 table .
Ø 1 table for Emp_id , Name and Dept (depends only on Emp_id)
Ø Another table for Emp_id , Mobile No (Depends on the Composite key)
Ø Eliminated Partial Dependency -

Ø Name, Dept depends only on Emp_id so they are moved to a seperate table.
Ø Mobile no depends on Composite key (Emp_id, Mobile No) so it remains in its own table.

Note:

Composite Primary key (Emp_id, Mobile No)

as Emp_id alone cannot uniquely identify a record with multiple phone numbers.
3NF:
it is in 2 NF
their is No transitive Dependency.

A transitive Dependency is when changing a non - key Column , might cause any other
column to change.

Note :

issue in 2 NF table transitive dependency .

Dept depends on Emp_id but if additional attribute of a Dept (Ex. Dept_location) . Dept will
have dependencies other than Emp_id . this violates 3 NF.
Steps to convert 3NF
Remove transitive Dependency . create a seperate table for Dept details and Dept as a primary
key.

Table -1 (3NF) Emp Info


Emp_id Name Dept
E1001 John IT
E1002 Smith Sales
E1003 Kim HR

Table -2 (3NF) Emp contact

Emp_id Mobile No
E1001 1234
E1002 6789
E1002 7890
E1003 6785
Table 3 - (3 NF) Dept Details

Dept Dept_Location
IT Pune
Sales Mumbai
HR Delhi
BCNF:

BCNF is an extension to the 3NF

In BCNF every functional Dependency A - > B , then A has be the superkey ( is uniquely
identifies rows in a table ) of that perticular table.
BCNF Violations

Table 1: Employee Information


1.Functional Dependency: Emp-id → {Name, Dept}
2.Emp-id is a superkey, so it satisfies BCNF.

Table 2: Employee Contact


1.Functional Dependency: Emp-id → Mobile
2.This works if each Emp-id has unique mobile numbers.
3.If multiple rows with the same Emp-id exist (as in E1002), Emp-id alone is not a superkey.
This violates BCNF.

Table 3: Department Details


1.Functional Dependency: Dept → Dept-Location
2.Dept is a superkey, so it satisfies BCNF.
Final BCNF Table
Table -1 (BCNF) Employee info

Emp_id Name Dept


E1001 John IT
E1002 Smith Sales
E1002 Kim HR

Table -2A (BCNF) Moblie Numbers by Employee

Emp_id Mobile No
E1001 1234
E1002 6789
E1003 7890
Table -2B (BCNF) Employees with Multiple Mobiles

Mobile No Emp_id
6789 E1002

7890 E1002

Table - 3 (BCNF) Dept Details

Dept Dept_Location
IT Pune
Sales Mumbai
HR Delhi
Explaination:

Decomposition of Table 2:

1.To address the BCNF violation, Mobile and Emp-id were split into separate tables:
Ø Table 2A ensures that Emp-id as a superkey identifies a unique Mobile.
Ø Table 2B tracks multiple mobile numbers for the same employee.

No BCNF Violations:

1.Now, all functional dependencies satisfy BCNF, as each determinant


(left-hand side of X→YX \to YX→Y) is a superkey.
The tables are now in BCNF, eliminating all redundancy and anomalies.
4 NF:

the Relation if it is in BCNF .

it has no multi valued dependency.

for a dependency A -> B if for a single value of A , Multi value of B exist then
relation will be multivalued dependency.
Identifying Multi-Valued Dependencies:

Table 2B (Employees with Multiple Mobiles):

1.The relationship between Mobile and Emp-id suggests a multi-valued dependency. Each
Emp-id can have multiple mobile numbers.

Table 1 (Employee Information):

1.There are no multi-valued dependencies in this table, as Name and Dept both depend on
Emp-id.

Table 3 (Department Details):

1.There are no multi-valued dependencies here, as Dept-Location depends only on Dept.


Final 4 NF Table

Table -1 (4NF) Employee info

Emp_id Name Dept


E1001 John IT
E1002 Smith Sales
E1002 Kim HR

Table -2A (4NF) Moblie Numbers by Employee

Emp_id Mobile No
E1001 1234
E1002 6789
E1003 7890
Table -2B1 (4NF) Employees Mobiles numbers

Emp_id Mobile No
E1002 6789
E1002 7890

Table -2B2 (4NF) Mobile owner

Mobile No Emp_id
6789 E1002

7890 E1002
Table 3 Dept Details

Dept Dept_Location
IT Pune
Sales Mumbai
HR Delhi
Explanation of Changes:

Multi-Valued Dependency Removed:

The multi-valued dependency in Table 2B (Mobile ↔ Emp-id) is resolved by splitting it into


two separate tables: Employee Mobile Numbers and Mobile Owner.
Preserved Data Integrity:

All original relationships are maintained without redundancy.


No Remaining MVDs:

Now, every non-trivial dependency involves attributes fully dependent on their respective
superkeys.
The tables are now in 4NF, ensuring no redundancy or anomalies due to multi-valued
dependencies.

You might also like