Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views122 pages

Complete SQL Basics Series in 1 Document File

The document provides a comprehensive overview of SQL basics, focusing on key concepts such as the differences between DROP and TRUNCATE statements, the use of aliases, constraints in SQL, and the distinctions between DDL and DML languages. It explains how DROP permanently removes a table while TRUNCATE only deletes the data but retains the table structure. Additionally, it covers the importance of constraints for data integrity and the roles of DDL and DML in managing database structures and data.

Uploaded by

satish.gsat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views122 pages

Complete SQL Basics Series in 1 Document File

The document provides a comprehensive overview of SQL basics, focusing on key concepts such as the differences between DROP and TRUNCATE statements, the use of aliases, constraints in SQL, and the distinctions between DDL and DML languages. It explains how DROP permanently removes a table while TRUNCATE only deletes the data but retains the table structure. Additionally, it covers the importance of constraints for data integrity and the roles of DDL and DML in managing database structures and data.

Uploaded by

satish.gsat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 122

Part - 3

SQL Basics
Interview
Questions and
Answers..!
es th
ns
wi
tio

in qu
g

ar
Sh ter
oun
C

Krishna kumar
@Krishan kumar
What is the difference between
DROP and TRUNCATE
statements?
How drop Works;

The DROP statement completely removes a


table from the database, including its data,
structure, and all associated elements like
indexes, constraints, and permissions.

Example:
DROP TABLE employees;

This command deletes the employees table


and everything related to it from the
database.

How TRUNCATE works


TRUNCATE;

The TRUNCATE statement deletes all rows in a


table but keeps the table structure intact.

It is typically faster than DELETE because it uses


fewer system resources and minimal logging.

Example:
TRUNCATE TABLE employees;

This command removes all data from the


employees table but leaves the table structure,
indexes, and constraints in place.

Difference between DROP and TRUNCATE


Difference;
DROP Removes the entire table, including its
structure and data.
TRUNCATE Removes only the data, keeping the table
structure.

DROP Can be slower due to the additional tasks of


deleting related objects and the table structure.
TRUNCATE is Faster than DELETE because it uses
minimal logging and doesn't delete the structure.

DROP Cannot be rolled back; once executed, it


permanently deletes the table.
TRUNCATE Typically cannot be rolled back; it
permanently deletes the data.

DROP Affects the database schema by removing the


table and its relationships.
TRUNCATE Does not affect the schema; only removes
data.

DROP is Used when you need to permanently delete a


table and all its contents.
TRUNCATE is used when you need to quickly clear all
data from a table but plan to reuse the table structure.
Example;

Scenario: You no longer need the employees


table and want to remove it completely from
the database.
Use DROP
DROP TABLE employees;

Scenario: You want to quickly clear out all


employee records but keep the table for future
use.
Use TRUNCATE
TRUNCATE TABLE employees;
wanna see some
Counter Questions
1. Can TRUNCATE trigger a DELETE
trigger in SQL?
No, TRUNCATE does not trigger a DELETE trigger
because it is not considered a row-level
operation.

It works on the table as a whole and bypasses


any DELETE triggers that might be set on the
table.

Next Question
2. What happens to the table’s
constraints and indexes when you
use TRUNCATE?
When we use TRUNCATE, the table’s constraints
and indexes remain intact.

The operation only removes the data, leaving the


table structure, including constraints, indexes,
and relationships, unchanged.

on
ti
es
Qu
ext
N
3. Is it possible to TRUNCATE a table
that is referenced by a foreign key?

No, you cannot TRUNCATE a table that is


referenced by a foreign key constraint.

The TRUNCATE operation requires that there be


no foreign key dependencies on the table, as it
cannot be rolled back and would violate
referential integrity.

n
tio
es
Qu
ext
N
4. How does the performance of
TRUNCATE compare to DELETE?

TRUNCATE is generally faster than DELETE


because it deallocates the data pages used to
store the table data, rather than logging each
row deletion individually.

This makes TRUNCATE more efficient for


clearing all data from a table, especially for large
tables.

re
mo
nts
wa
Ciku
5. When should you use DROP versus
TRUNCATE?

Use DROP when you want to permanently delete


a table and all its contents from the database.

Use TRUNCATE when you want to quickly clear


out all data from a table but keep the table
structure intact for future use.

you completed one interview question


with me,
can you do me a favour
Part - 4

SQL Basics
Interview
Questions and
Answers..!
es th
ns
wi
tio

in qu
g

ar
Sh ter
oun
C

Krishna kumar
@Krishan kumar
What is Alias in SQL?
What is Alias

An alias in SQL is a temporary name given to a


table or column within a SQL query to make it
easier to refer to, especially when names are
long or complex. It's similar to giving a
nickname to make things simpler.

Why are aliases used in SQL?


Why it aliases used

Aliases are used to make SQL queries more


readable and concise.

They help to shorten long table or column names


and are particularly useful when dealing with
complex queries involving multiple tables or
columns with similar names.

Do aliases change the actual names of tables or columns in the database?


Alias in database

No, aliases are temporary and only exist during


the execution of a query.

They do not change the actual names of the


columns or tables in the database.

Once the query execution is complete, the aliases


are no longer in effect.

How do you create an alias for a column in SQL?


Create an alias for a
column in SQL?

To create an alias for a column, you use the AS


keyword followed by the alias name.

For example,

SELECT customer_id AS cid FROM customer;

Here, cid is the alias for the customer_id column.


wanna see some
Counter Questions
1. Can you use aliases without the
AS keyword in SQL?
Yes, in many SQL databases, you can use aliases
without the AS keyword. For example,

SELECT customer_id cid FROM customer;

will work the same as using AS. However, using


AS makes the query more readable and is often
considered a best practice.

Next Question
2. Can you use aliases in the WHERE
clause of a SQL query?

No, you cannot use column aliases directly in the


WHERE clause because the WHERE clause is
evaluated before the SELECT clause.

However, you can use aliases in the ORDER BY


and GROUP BY clauses since these are
evaluated after the SELECT clause.

on
ti
es
Qu
ext
N
3. Are aliases case-sensitive in
SQL?

It depends on the SQL database being used. In


some databases, aliases are case-insensitive by
default, meaning CID and cid would be
considered the same.

In others, aliases might be case-sensitive,


requiring the use of consistent case or quotation
marks.

n
tio
es
Qu
ext
N
4. Can you use an alias in the HAVING
clause of a SQL query?

Yes, you can use column aliases in the HAVING


clause because the HAVING clause is evaluated
after the GROUP BY clause and after the SELECT
clause, where aliases are defined.

This makes them accessible in HAVING


conditions.

re
mo
nts
wa
Ciku
5. Can you alias multiple columns in a
SQL query? How?

Yes, you can alias multiple columns in a SQL


query by specifying each alias individually.

For example:

you completed one interview question


with me,
can you do me a favour
Part - 5

SQL Basics
Interview
Questions and
Answers..!
es th
ns
wi
tio

in qu
g

ar
Sh ter
oun
C

Krishna kumar
@Krishan kumar
What is the similarities
between DROP and
TRUNCATE ?
Why they are used in SQL

Both DROP and TRUNCATE are used to remove


data from a database.

TRUNCATE removes all rows from a table


but keeps the table structure intact, while

DROP removes the entire table along with its


structure and data.

Are DROP and TRUNCATE operations reversible?


DROP and TRUNCATE

No, both DROP and TRUNCATE are irreversible


operations.

Once executed, the changes cannot be undone,


and

the data cannot be recovered without a backup.


They are considered destructive commands.

How do DROP and TRUNCATE differ in terms of what they remove?


DROP and TRUNCATE

TRUNCATE removes all rows from a table but


retains the table structure for future use.

In contrast, DROP removes both the table's data


and its structure, effectively deleting the table
from the database.

Why are DROP and TRUNCATE considered faster than DELETE?


BIG YES;

Both DROP and TRUNCATE are faster than


DELETE because they do not log individual row
deletions.

TRUNCATE deallocates data pages used by the


table, and

DROP removes the entire table definition, which


requires less processing.

When should you use TRUNCATE instead of DROP?


DROP and TRUNCATE

Use TRUNCATE when you want to quickly


remove all rows from a table but plan to reuse
the table structure.

Use DROP when you want to permanently


delete a table and its structure from the
database.

wanna see some


Counter Questions
1. Can you TRUNCATE a table that
has a foreign key constraint?
No, you cannot TRUNCATE a table if it is
referenced by a foreign key constraint.

You would need to either drop the foreign key


constraint first or use a DELETE statement to
remove rows while maintaining referential
integrity.

Next Question
2. Does TRUNCATE reset identity
columns in a table?

Yes, TRUNCATE typically resets the identity


seed of an identity column to its initial value,

unlike DELETE, which does not affect the identity


seed.

on
ti
es
Qu
ext
N
3. Can DROP be used to remove other
database objects besides tables?

Yes, DROP can be used to remove other


database objects such as views, stored
procedures, indexes, and even entire databases.

The syntax changes slightly depending on the


object being dropped

(e.g., DROP VIEW view_name;).

n
tio
es
Qu
ext
N
4. How does TRUNCATE handle
triggers on a table?

TRUNCATE does not activate triggers defined on


a table.

If there are any triggers that you want to execute


during data deletion, you should use the DELETE
statement instead.

re
mo
nts
wa
Ciku
5. Is it possible to TRUNCATE or
DROP a table within a transaction?

In most database systems, you can TRUNCATE a


table within a transaction, but the effect might
not be rolled back, depending on the system.

DROP operations are usually allowed within a


transaction and can be rolled back if the
transaction is not committed.

you completed one interview question


with me,
can you do me a favour
Part - 6

SQL Basics
Interview
Questions and
Answers..!
es th
ns
wi
tio

in qu
g

ar
Sh ter
oun
C

Krishna kumar
@Krishan kumar
What are the constraints in
SQL?
What are constraints in SQL, and
why are they important?

Constraints in SQL are rules that enforce


restrictions on the data in a table. They are
essential for
ensuring data integrity,
consistency, and
accuracy within a database.

Constraints prevent invalid data entry and help


maintain reliable relationships between tables.

What is a Primary Key Constraint?


Primary Key Constraint

A Primary Key Constraint ensures that each


record in a table is uniquely identifiable.

It means that values in the primary key column(s)


must be unique and cannot be NULL.

For example: a student_id column in a students


table could be defined as a primary key.

How does a Foreign Key Constraint work?


Foreign Key Constraint work

A Foreign Key Constraint establishes a


relationship between two tables.

It ensures referential integrity by requiring that


values in a column match values in another
table's primary key or unique key.

This constraint prevents invalid data from being


entered into a table, maintaining consistent
relationships.

What is the purpose of a Unique Constraint?


Purpose of a Unique Constraint

A Unique Constraint ensures that all values in a


column (or a group of columns) are unique
across the table.

Unlike the primary key, a unique constraint


allows NULL values, but only one NULL value is
permitted in a column.

It's used to enforce uniqueness without setting


a column as a primary key.

How does a Check Constraint help maintain data integrity?


Check Constraint in maintain
data integrity
A Check Constraint defines a condition that
must be met for values being inserted or
updated in a column.

It restricts the range or type of acceptable


values for that column, ensuring that only valid
data is entered.

For instance: a check constraint on a salary


column might enforce that salaries are non-
negative.

What is the Not Null Constraint in SQL?


Not Null Constraint in SQL

The Not Null Constraint ensures that a column


cannot contain NULL values.

It requires that every row in the table has a


value for that column.

This constraint is commonly used for fields that


must always have data, like product_name in a
products table

wanna see some


Counter Questions
1. Can a table have more than one
Primary Key Constraint?
No, a table can only have one primary key
constraint.

However, the primary key can consist of multiple


columns (a composite key) to ensure unique
identification of records.

Next Question
2. What is the difference between a
Unique Constraint and a Primary Key
Constraint?
Both ensure uniqueness of values in a column,
but a primary key constraint does not allow
NULL values and uniquely identifies each record
in a table.

A unique constraint, on the other hand, can allow


one NULL value per column and does not
necessarily uniquely identify each record.

on
ti
es
Qu
ext
N
3. What happens if you try to delete a row
in a table that is referenced by a Foreign
Key Constraint in another table?

If a foreign key constraint exists, attempting to


delete a referenced row will result in an error
unless cascading options are defined.

(ON DELETE CASCADE or ON DELETE SET NULL)

These options either delete the related records


or set the foreign key to NULL in the referencing
table.

n
tio
es
Qu
ext
N
4. How do you modify or remove a
constraint from a table in SQL?
To modify or remove a constraint, you typically
use the ALTER TABLE statement.

For instance, to remove a check constraint


named chk_salary, you would use:

ALTER TABLE employees DROP CONSTRAINT chk_salary;

To add or modify a constraint, the ALTER TABLE


statement can also include ADD CONSTRAINT
clauses.

re
mo
nts
wa
Ciku
5. Can you have a Check Constraint
on multiple columns in SQL?

Yes, a check constraint can be defined on


multiple columns.

For example: you can ensure that the start_date


is always before the end_date in a table by using
a check constraint that compares these two
columns.

you completed one interview question


with me,
can you do me a favour
Part - 7

SQL Basics
Interview
Questions and
Answers..!
es th
ns
wi
tio

in qu
g

ar
Sh ter
oun
C

Krishna kumar
@Krishan kumar
What are DDL and DML
languages?
What are DDL and
DML in SQL?

DDL (Data Definition Language) and DML (Data


Manipulation Language) are two subsets of SQL
used for different purposes.

DDL defines and modifies the structure of a


database, while

DML manipulates and manages the data


within the database.

What does DDL stand for, and what is its purpose?


DDL and its purpose?

DDL stands for Data Definition Language. It is


used to define the structure or schema of a
database. Commands like
CREATE,
ALTER, and
DROP

Are part of DDL and are used by database


designers or administrators to create, modify, or
delete database structures.

Can you explain what DML is and its primary function?


DML is and its primary function;

DML stands for Data Manipulation Language. It is


used to manipulate and manage data within an
existing database.
Common DML commands include
SELECT,
INSERT,
UPDATE, and
DELETE

which allow users to retrieve, add, modify, or


remove data from a database.

What is the difference between Procedural DML and Non-Procedural DML?


Difference between Procedural
DML and Non-Procedural DML
Procedural DML requires the user to specify
both the data they want and the procedure to
obtain it.

In contrast, Non-Procedural DML only requires


users to specify what data they need, and the
system determines how to retrieve it.

Non-Procedural DML is generally simpler and


easier to use.

Who typically uses DDL commands, and why?


Who typically uses DDL
commands, and why?
DDL commands are typically used by database
designers or database administrators (DBAs).

These commands are not meant for end-users

because they deal with defining and altering


the database structure,

which requires a deep understanding of the


database system.

wanna see some


Counter Questions
1. What are some examples of DDL
commands, and what do they do?
Common DDL commands include:
CREATE: Creates a new table or database.

ALTER: Modifies an existing table structure,


such as adding or dropping a column.

DROP: Deletes a table or database entirely.

TRUNCATE: Removes all rows from a table


without deleting the table structure itself.

Next Question
2. How does DML differ from DDL in
terms of database operations?
DML deals with data manipulation within the
existing structure, such as
inserting,
updating, or
deleting data.

DDL, on the other hand, is concerned with


defining, altering, and managing the structure of
the database itself, not the data inside it.

on
ti
es
Qu
ext
N
3. What are the advantages of Non-
Procedural DML over Procedural DML?

Non-Procedural DML is more user-friendly


because it only requires users to specify what
data they want without worrying about how to
get it.

This makes it easier to learn and use, especially


for beginners, and allows the system to optimize
query execution.

n
tio
es
Qu
ext
N
4. Why is it important to distinguish
between DDL and DML in database
management?
Understanding the difference is crucial because
DDL commands affect the database schema and
are generally irreversible without backups,

while DML commands manipulate the data within


that structure. Misusing these commands can
lead to data loss or structural issues in the
database.

re
mo
nts
wa
Ciku
5. What is the significance of the
COMMIT and ROLLBACK
commands in DML?
COMMIT and ROLLBACK are transaction control
commands used in conjunction with DML.

COMMIT saves all changes made by DML


commands to the database permanently, while

ROLLBACK undoes those changes if necessary.


They are essential for ensuring data integrity
during transactions.

you completed one interview question


with me,
can you do me a favour
Part - 9

SQL Basics
Interview
Questions and
Answers..!
es th
ns
wi
tio

in qu
g

ar
Sh ter
oun
C

Krishna kumar
@Krishan kumar
What is the difference
between DISTINCT and
GROUP BY?
Distinct;

Distinct purpose is to remove duplicate rows from


the result set. Ensures each row in the result set
is unique based on the selected columns.

Means:

Let's understand it with a example


Example;
Suppose you have a list of fruits with duplicates:

[ apple, banana, apple, orange, banana.]

Using DISTINCT will give you a list with no


duplicates: apple, banana, orange

What about GROUP BY


GROUP BY:
The purpose is to rows that have the same values
in specified columns into summary rows.
Typically used with aggregate functions like
COUNT, SUM, AVG, etc.
Means:

Let's understand it with a example


Example;
Suppose you have the same list of fruits:

[ apple, banana, apple, orange, banana.]

Using GROUP BY with COUNT will give you the


number of each fruit:

apple - 2,
banana - 2,
orange - 1.
wanna see some
Counter Questions
1. When should you use DISTINCT
instead of GROUP BY?
Use DISTINCT when you want to remove
duplicate rows from the result set based on one
or more columns.

DISTINCT is typically used when you want to


return only unique values for the specified
columns without any aggregation or grouping.

Next Question
2. Can you use GROUP BY without an
aggregate function? What happens in
that case?
Yes, you can use GROUP BY without an
aggregate function. In that case, GROUP BY
simply returns a single row for each unique
combination of values in the grouped columns.

However, using it without an aggregate function


is uncommon, as GROUP BY is primarily used for
aggregation.

on
ti
es
Qu
ext
N
3. How does the performance of
DISTINCT compare to GROUP BY?
Performance depends on the context, but
generally, DISTINCT can be more efficient than
GROUP BY when you're only removing duplicates
without needing to perform any aggregation.

GROUP BY might require additional computation,


especially if aggregate functions are involved.

n
tio
es
Qu
ext
N
4. What happens if you apply
DISTINCT to a query that already
uses GROUP BY?
Applying DISTINCT to a query that uses GROUP
BY is redundant

because GROUP BY already ensures that each


group of rows is unique based on the grouped
columns.

DISTINCT would have no additional effect and


could potentially slow down the query.

re
mo
nts
wa
Ciku
5. Can you combine DISTINCT with
aggregate functions? How would
that work?
Yes, you can combine DISTINCT with aggregate
functions.

For example, COUNT(DISTINCT column_name)


counts the number of unique, non-null values in
the specified column.

This is useful when you want to apply


aggregation only to distinct values in a column.

you completed one interview question


with me,
can you do me a favour
Part -10

SQL Basics
Interview
Questions and
Answers..!
es th
ns
wi
tio

in qu
g

ar
Sh ter
oun
C

Krishna kumar
@Krishan kumar
What is the difference between
WHERE and HAVING clauses in
SQL?
Where:
The WHERE clause in SQL is used to filter rows
before any grouping takes place.

It applies conditions to individual rows in the


table,

allowing you to specify which rows you want to


include in your results.

Let's understand it with a example


Example;
if you have a list of employees and you want to
find only those in the Sales department, you
would use the WHERE clause like this:

SELECT * FROM Employees


WHERE Department = 'Sales

This command filters the data at the row level,


retrieving only the rows that meet the specified
condition.

What about HAVING


Having:

The HAVING clause is used to filter groups after


the GROUP BY clause has been applied.

This means it works on the aggregated data


rather than on individual rows.

It is typically used in conjunction with aggregate


functions like COUNT, SUM, or AVG.

Let's understand it with a example


Example;
if you want to find departments that have more
than 10 employees, you would use the HAVING
clause

SELECT Department,
COUNT(*) FROM Employees
GROUP BY Department
HAVING COUNT(*) > 10;

This filters the results after grouping by


department

Ensuring that only groups meeting the condition


appear in the final output. Thus, WHERE is for
row-level filtering and HAVING is for group-level
filtering.
wanna see some
Counter Questions
1. Can you use the HAVING clause
without a GROUP BY clause? What
happens?
Yes, you can use the HAVING clause without a
GROUP BY clause, but it behaves like a WHERE
clause in that context.

However, it is uncommon to do so since HAVING


is generally used with aggregate functions.

Next Question
2. What would happen if you try to
use an aggregate function in the
WHERE clause?
Using an aggregate function in the WHERE
clause will result in an error because WHERE is
evaluated before aggregation occurs.

Aggregate functions should be used in the


HAVING clause.

on
ti
es
Qu
ext
N
3. Can WHERE and HAVING clauses
be used together in the same query?
How?
Yes, WHERE and HAVING clauses can be used
together in the same query.

The WHERE clause filters rows before


aggregation, and the HAVING clause filters the
aggregated results.

For example:
4. If you need to filter results based
on an aggregate condition, why can't
you use WHERE instead of HAVING?
WHERE cannot be used to filter results based on
an aggregate condition because WHERE is
applied before aggregation.

HAVING is necessary because it filters the


results after aggregation

re
mo
nts
wa
Ciku
5.Is it possible to use WHERE and
HAVING clauses to filter the same
condition? How would that work?
It's possible but uncommon to filter the same
condition with both WHERE and HAVING.
Typically, WHERE is used to filter raw data
before aggregation, and HAVING refines the
result after aggregation. However, if you need to
apply the same condition before and after
aggregation

it can be done like this:


Find This Useful

Time to hit that like button


and give it some love! Z

Visit my Linkedin for such amazing Content€


krishan kumar

You might also like