SQL
SQL
Page 2 of 9
iNeuron Intelligence Pvt Ltd
3. What is a cursor?
Ans. The rows (one or more) that a SQL statement returns are stored
in a cursor. You can give a cursor a name so that a program can use it
to retrieve and handle the rows returned by the SQL statement one
at a Nme. Two different types of cursors exist - Implicit cursors and
Explicit cursors.
Page 3 of 9
iNeuron Intelligence Pvt Ltd
Page 4 of 9
iNeuron Intelligence Pvt Ltd
Page 5 of 9
iNeuron Intelligence Pvt Ltd
Page 6 of 9
iNeuron Intelligence Pvt Ltd
Page 7 of 9
iNeuron Intelligence Pvt Ltd
● The rows in the result set are then subjected to the HAVING clause.
The query output only contains the groups that saNsfy the HAVING
requirements.
19. How can you find the second highest salary from the given
employee table?
Query- SELECT MAX(emp_salary) FROM Employee WHERE SALARY <
(SELECT MAX(emp_salary) FROM Employee);
Page 8 of 9
iNeuron Intelligence Pvt Ltd
20. How can we copy the enNre data from one table to another in
SQL?
Query- INSERT INTO new_table SELECT * FROM old_table;
Page 9 of 9
iNeuron Intelligence Pvt Ltd
ADD
AUGMENT
INSERT
CREATE
Ans: C
3. What command will you use to remove rows from a table 'AGE'?
Page 2 of 5
iNeuron Intelligence Pvt Ltd
7. Define a view?
Page 3 of 5
iNeuron Intelligence Pvt Ltd
Standardizaaon
Data Integrity
Both a and b
None of the above
Ans: B
DDL
DML
DQL
DCL
Ans: A
IN and NOT IN
NOT IN only
LIKE only
IN only
Ans: C
Page 4 of 5
iNeuron Intelligence Pvt Ltd
13. Choose the correct order of keywords for SQL SELECT statements
brackets []
braces {}
parenthesis ()
none of the above
Ans: C
report
form
table
file
Ans: c
Page 5 of 5
Page | 1
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
The table is the set of organized data stored in columns and rows. Each
database table has a specified number of columns known as fields and several
rows which are called records. For example:
Table: Student
Field: Std ID, Std Name, Date of Birth
Data: 23012, William, 10/11/1989
The primary key is the combination of fields based on implicit NOT NULL
constraint that is used to specify a row uniquely. A table can have only one
primary key that can never be the NULL.
Page | 2
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
The unique key is the group of one or more fields or columns that uniquely
identifies the database record. The unique key is the same as a primary key
but it accepts the null value.
The foreign key is used to link two tables together and it is a field that refers
to the primary key of another table.
As the name indicates, join is the name of combining columns from one or
several tables by using common values to each. Whenever the joins are used,
the keys play a vital role.
Page | 3
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q10. What are the types of join and explain each?
Depending on the relationship between tables, join has the following types:
Inner Join
It returns rows when there is at least one match of rows between the tables.
Right Join
It returns the common rows between the tables and all rows of the Right-hand
side table. In other words, it returns all the rows from the right-hand side table
even there is no matches in the left-hand side table.
Left Join
It returns the common rows between the tables and all rows of the left-hand
side table. In other words, it returns all the rows from the left-hand side table
even there are no matches in the right-hand side table.
Full Join
As the name indicates, full join returns rows when there are matching rows in
any one of the tables. It combines the results of both left and right table
records and it can return very large result-sets.
The index is the method of creating an entry for each value in order to retrieve
the records from the table faster. It is a valuable performance tuning method
used for faster retrievals.
Page | 4
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q13. What are all the different normalizations?
Page | 5
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q15. What are all the different types of indexes?
Unique Index
The unique index ensures the index key column has unique values and it
applies automatically if the primary key is defined. In case, the unique index
has multiple columns then the combination of values in these columns should
be unique.
Clustered Index
Clustered index reorders the physical order of a table and searches based on
its key values. Keep in mind, each table can have only one clustered index.
Non-Clustered Index
Non-Clustered Index does not alter the physical order but maintains a logical
order of table data. Each table in the non-clustered index can have 999
indexes.
Page | 8
Page | 1
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q1. Explain DML with its examples?
SELECT INTO: is used to select data from one table and insert into another
table.
INSERT: is used to insert data or records into a table.
DELETE: is used to delete records from any database table.
UPDATE: is used to update the values of different records in the database
This type of short queries are asked in the SQL interview questions so that
they can understand about the candidate’s knowledge and hands-on
experience.
Page | 2
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q4. What is a stored procedure?
For example, when a new employee is added to the employee database, new
records should be created in the related tables like Salary, Attendance, and
Bonus tables.
TRUNCATE command removes all the table rows permanently that can
never be rolled back. DELETE command is also used to remove the rows
from the table but commit and rollback can be performed after the deletion.
Moreover, the WHERE clause is also used as conditional parameters.
Q7. What is the main difference between local and global variables?
As the name indicates, the local variables can be used inside the function but
global ones are used throughout the program. Local variables are only limited
to the function and cannot be referred or used with other functions.
Page | 3
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q8. What is the main difference between local and global variables?
As the name indicates, the local variables can be used inside the function but
global ones are used throughout the program. Local variables are only limited
to the function and cannot be referred or used with other functions.
Q9. What is a constraint and what are the common SQL constraints?
SQL constraint is used to specify the rules for data in a table and can be
specified while creating or altering the table statement.
NOT NULL
CHECK
DEFAULT
UNIQUE
PRIMARY KEY
FOREIGN KEY
Page | 4
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q12. What is the difference between the Cluster and Non-Cluster Index?
The clustered index defines the order in which data is physically stored in a
database table and used for easy retrieval by altering the way that the records
are stored.
The non-clustered index improves the speed of data retrieval from database
tables but stores data separately from the data rows. It makes a copy of
selected columns of data with the links to the associated table.
Q13. What is Datawarehouse?
A self-join SQL query is used to compare to itself and values in a column are
compared with other values in the same column in the same database table
Q15. What is Cross-Join?
Cross-join is used to generate a paired combination of each row of table A
with each row of table B. If the WHERE clause is used in cross join then the
SQL query will work like an INNER JOIN.
Page | 5
iNeuron Intelligence Pvt Ltd
Q17. What are all types of user-defined functions?
Following are the user-defined functions:
Collation provides a set of rules that determine how character data can be
sorted and compared in a database. It also provides the case and accent
sensitivity properties. Collation is used to compare A and other language
characters with the help of ASCII value.
Case Sensitivity (A & a, B & b or any other uppercase and lowercase letters)
Accent Sensitivity
Kana Sensitivity (Japanese Kana characters)
Width Sensitivity (Single byte character (half-width) & same character
represented as a double-byte character)
Page | 6
Page | 1
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
COMMIT command is used to save all the transactions to the database since
the last COMMIT or ROLLBACK command where a transaction is a unit of
work performed against any database.
Consider an example of the deletion of those records which have age = 25 and
then COMMIT the changes in the database.
SQL> COMMIT;
Page | 2
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q4. What is CLAUSE?
SQL clause is used to filter some rows from the whole set of records with the
help of different conditional statements. For example, WHERE and HAVING
conditions.
Q5. What is the difference between null, zero and blank space?
Null is neither same as zero nor blank space because it represents a value that
is unavailable, unknown, or not applicable at the moment. Whereas zero is a
number and blank space belongs to characters.
It is the same as stored procedures but it calls by itself until it reaches any
boundary condition. So, it is the main reason why programmers use recursive
stored procedures to repeat their code any number of times.
Union operator is used to combining the results of two tables and removes the
duplicate rows. Minus is used to return matching records of the first and
second queries and other rows from the first query but not by the second one.
The Intersect operator is used to return the common rows returned by the two
select statements.
FROM table_name;
Page | 3
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q9. What are aggregate and scalar functions?
10. How can you create an empty table from an existing table?
With the help of the following command, we can create an empty table from
an existing table with the same structure with no rows copied:
Page | 4
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Q13. How to select unique records from a table?
Apply the following SQL query to select unique records from any table:
Q14. What is the command used to fetch first 4 characters of the string?
Following are the ways to fetch the first 4 characters of the string:
The primary key that is created on more than one column is known as composite
primary key.
% (percent sign) Matches zero or more characters and _ (Underscore) is used for
matching exactly one character. For example:
Page | 5
iNeuron Intelligence Pvt Ltd
___________________________________________________________________________________________
Group functions are mathematical functions used to work on the set of rows
and return one result per group. AVG, COUNT, SUM, VARIANCE, MAX,
MIN are most commonly used Group functions.
The entity is the name of real-world objects either tangible or intangible and it is
the key element of relational databases. For a database entity, workflow and tables
are optional but properties are necessary. For example: In the database of any
institute, students, professors, workers, departments and projects can be known as
entities. Each entity has associated properties that offer it an identity.
The relationship is the name of links or relations between entities that have
something to do with each other. For example, the employee table should be
associated with the salary table in the company’s database.
Group functions are mathematical functions used to work on the set of rows
and return one result per group. AVG, COUNT, SUM, VARIANCE, MAX,
MIN are most commonly used Group functions.
Q20. What is the MERGE statement?
Page | 6
Page | 1
iNeuron Intelligence Pvt Ltd
1. What is Database?
Ans. A database is an organized collection of structured information, or data,
typically stored electronically in a computer system. A database is usually
controlled by a database management system (DBMS).
2. What is SQL?
Ans. SQL is a structured query language. It is a standard language for
accessing and manipulating databases.
Page | 2
iNeuron Intelligence Pvt Ltd
Ans. A primary key is a unique identifier for each record in a table whereas a
foreign key establishes a relationship between tables by referencing the
primary key of another table.
Ans. a primary key is a key that uniquely identifies each record in a table but
cannot store NULL values. In contrast, a unique key prevents duplicate values
in a column and can store NULL values.
Ans. char() stores character of fixed length and the maximum length currently
is 255 bytes whereas varchar() is used when the sizes of the column data
entries vary considerably. varchar() columns can be up to 65,535 bytes.
10. What is the main difference between update and alter commands in sql?
Ans. The main difference between the two is that the ALTER command adds,
deletes, modifies, renames the attributes of the relation, and the UPDATE
command modifies the values of the records in the relations.
Page | 3
iNeuron Intelligence Pvt Ltd
12. Suppose you have a table named person how would you enter record in
this table?
Ans.
Ans. The DROP Command drops the complete table from the database.
The DELETE command deletes one or more existing records from the table
in the database.
The TRUNCATE Command deletes all the rows from the existing table,
leaving the row with the column names.
15. Suppose you have a table named language and you only want to retrieve
name column from it how will you proceed.
Ans.
Page | 4
iNeuron Intelligence Pvt Ltd
16. Write a query to retrieve unique values from the table flim_actor and sort it.
Ans.
17) how you would find all customers with the last name “Smith”:
Ans.
Page | 5
iNeuron Intelligence Pvt Ltd
__________________________________________________________________
Ans. The WHERE clause is used to filter records. It is used to extract only those
records that fulfill a specified condition.
Ans. The main difference between WHERE and HAVING clause is that the
WHERE clause allows you to filter data from specific rows (individual rows) from
a table based on certain conditions. Whereas the HAVING clause allows you to
filter data from a group of rows in a query based on conditions involving aggregate
values.
Ans.
Page | 6
Page | 1
iNeuron Intelligence Pvt Ltd
Ans. when multiple tables are joined in a single query, you need a way to
identify which table you are referring to when you reference columns in
the select, where, group by, having, and order by clauses. So, by defining
alias we can refer to the table easily with a shortcut name.
2) Write a query to retrieve the records from film table whose rating is g and
are available for more than 7.
Ans.
Ans. The AND and OR operators are used to filter records based on more
than one condition: The AND operator displays a record if all the conditions
separated by AND are TRUE. The OR operator displays a record if any of the
conditions separated by OR is TRUE.
4) What is the order of execution in sql?
Ans. 1. From
2. Where
3. Groupby
4. Having
5. Select
6. Order by
7. Limit
Page | 2
iNeuron Intelligence Pvt Ltd
5) How can you use both AND & OR commands at the same time?
Ans. You should use parentheses to group conditions together. The next query
specifies that only those films that are rated G and are available for 7 or more
days, or are rated PG-13 and are available 3 or fewer days, be included in the
result set:
6) Retrieve the actor ID, first name, and last name for all actors. Sort by last
name and then by first name.
Ans.
7) Retrieve the actor ID, first name, and last name for all actors whose last
name equals 'WILLIAMS' or 'DAVIS'.
Ans.
Page | 3
iNeuron Intelligence Pvt Ltd
______________________________________________________________
8) Write a query against the rental table that returns the IDs of the customers
who rented a film on July 5, 2005 (use the rental.rental_date column, and you
can use the date() function to ignore the time component). Include a single
row for each distinct customer ID.
Ans.
Ans. NOT is a logical operator in SQL that you can put before any
conditional statement to select rows for which that statement is false.
Ans. When you have both an upper and lower limit for your range, you may
choose to use a single condition that utilizes the between operator rather than
using two separate conditions.
Ans.
Page | 4
iNeuron Intelligence Pvt Ltd
______________________________________________________________
12) Write a query where whose last name falls in between FA and FR:
Ans.
Ans. With the in operator, you can write a single condition no matter how
many expressions are in the set.
Ans. In a table, there are rows and columns, with rows referred to as records
and columns referred to as fields.
15) Write a query for retrieving all the customers whose last_name starts with
‘Q’ or ‘Y
Ans.
Page | 5
iNeuron Intelligence Pvt Ltd
______________________________________________________________
Ans.
Ans.
18) Construct a query that retrieves all rows from the payments table where
the amount is either 1.98, 7.98, or 9.98
Ans.
Page | 6
iNeuron Intelligence Pvt Ltd
______________________________________________________________
19) Construct a query that finds all customers whose last name contains
an A in the second position and a W anywhere after the A.
Ans.
Ans. Join clause is used to combine two or more rows based on common
columns.
41) How many types of Joins are there?
Ans. 1. Inner Join
2. Left join
3. Right join
4. Full outer join
5. Self join
Page | 7