0% found this document useful (0 votes)
18 views4 pages

SQL Store

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

SQL Store

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

What is a Stored Procedure?

-->A stored procedure is a prepared SQL code that you can save, so the code can be
reused over and over again.

Stored Procedure Syntax


CREATE PROCEDURE procedure_name
AS
sql_statement
GO;

Execute a Stored Procedure


EXEC procedure_name;

https://www.w3schools.com/sql/sql_stored_procedures.asp

--ROW_NUMBER function is a SQL ranking function that assigns a sequential rank


number to each new record in a partition
--The RANK() function in the result set defines the rank of each row within your
ordered partition. If both rows have the same rank,
the next number in the ranking will be the previous rank plus a number of
duplicates.
--The DENSE_RANK() function assigns a distinct rank to each row within a partition
based on the provided column value, with no gaps.

Q38. How to remove duplicate rows in SQL?

If the SQL table has duplicate rows, the duplicate rows must be removed.

Let’s assume the following table as our dataset:

ID Name Age
1 A 21
2 B 23
2 B 23
4 D 22
5 E 25
6 G 26
5 E 25
The following SQL query removes the duplicate ids from the table:

DELETE FROM table WHERE ID IN (


SELECT
ID, COUNT(ID)
FROM table
GROUP BY ID
HAVING
COUNT (ID) > 1);

Q19. Write the SQL query to get the third maximum salary of an employee from a
table named employees.
Employee table

employee_name salary
A 24000
C 34000
D 55000
E 75000
F 21000
G 40000
H 50000

SELECT * FROM(

SELECT employee_name, salary, DENSE_RANK()

OVER(ORDER BY salary DESC)r FROM Employee)

WHERE r=&n;

To find 3rd highest salary set n = 3

Q7. What are some common clauses used with SELECT query in SQL?
The following are some frequent SQL clauses used in conjunction with a SELECT
query:

WHERE clause: In SQL, the WHERE clause is used to filter records that are required
depending on certain criteria.
ORDER BY clause: The ORDER BY clause in SQL is used to sort data in ascending (ASC)
or descending (DESC) order depending on specified field(s) (DESC).
GROUP BY clause: GROUP BY clause in SQL is used to group entries with identical
data and may be used with aggregation methods to obtain summarised database
results.
HAVING clause in SQL is used to filter records in combination with the GROUP BY
clause. It is different from WHERE, since the WHERE clause cannot filter aggregated
records.

Q8. What are UNION, MINUS and INTERSECT commands?


The UNION operator is used to combine the results of two tables while also removing
duplicate entries.
The MINUS operator is used to return rows from the first query but not from the
second query.
The INTERSECT operator is used to combine the results of both queries into a single
row.
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.

Q18. Explain character-manipulation functions? Explains its different types in SQL.


Change, extract, and edit the character string using character manipulation
routines. The function will do its action on the input strings and return the
result when one or more characters and words are supplied into it.

The character manipulation functions in SQL are as follows:

A) CONCAT (joining two or more values): This function is used to join two or more
values together. The second string is always appended to the end of the first
string.

B) SUBSTR: This function returns a segment of a string from a given start point to
a given endpoint.

C) LENGTH: This function returns the length of the string in numerical form,
including blank spaces.
Q20. What is the difference between the RANK() and DENSE_RANK() functions?
The RANK() function in the result set defines the rank of each row within your
ordered partition. If both rows have the same rank,
the next number in the ranking will be the previous rank plus a number of
duplicates. If we have three records at rank 4, for example, the next level
indicated is 7.

The DENSE_RANK() function assigns a distinct rank to each row within a partition
based on the provided column value, with no gaps.
It always indicates a ranking in order of precedence. This function will assign the
same rank to the two rows if they have the same rank,
with the next rank being the next consecutive number. If we have three records at
rank 4, for example, the next level indicated is 5

D) INSTR: This function calculates the precise numeric location of a character or


word in a string.

E) LPAD: For right-justified values, it returns the padding of the left-side


character value.

F) RPAD: For a left-justified value, it returns the padding of the right-side


character value.

G) TRIM: This function removes all defined characters from the beginning, end, or
both ends of a string. It also reduced the amount of wasted space.

H) REPLACE: This function replaces all instances of a word or a section of a string


(substring) with the other string value specified.

Q69. How many Aggregate functions are available in SQL?

SQL aggregate functions provide information about a database’s data. AVG, for
example, returns the average of a database column’s values.

SQL provides seven (7) aggregate functions, which are given below:

AVG(): returns the average value from specified columns.


COUNT(): returns the number of table rows, including rows with null values.
MAX(): returns the largest value among the group.
MIN(): returns the smallest value among the group.
SUM(): returns the total summed values(non-null) of the specified column.
FIRST(): returns the first value of an expression.
LAST(): returns the last value of an expression.

Q72. What are the syntax and use of the COALESCE function?

From a succession of expressions, the COALESCE function returns the first non-NULL
value. The expressions are evaluated in the order
that they are supplied, and the function’s result is the first non-null value. Only
if all of the inputs are null does the COALESCE method return NULL.

The syntax of COALESCE function is COALESCE (exp1, exp2, …. expn)

Q88. What is the need for MERGE statement?


This statement allows conditional update or insertion of data into a table. It
performs an UPDATE if a row exists, or an INSERT if the row does not exist.
Q89. What do you mean by recursive stored procedure?
Recursive stored procedure refers to a stored procedure which calls by itself until
it reaches some boundary condition. This recursive function or procedure helps
the programmers to use the same set of code n number of times.

Q102. How can you fetch first 5 characters of the string?


There are a lot of ways to fetch characters from a string. For example:

Select SUBSTRING(StudentName,1,5) as studentname from student

Q104. What is a View?


A view is a virtual table which consists of a subset of data contained in a table.
Since views are not present, it takes less space to store.
View can have data of one or more tables combined and it depends on the
relationship.

Let’s move to the next question in this SQL Interview Questions.

Q105. What are Views used for?


A view refers to a logical snapshot based on a table or another view. It is used
for :

Restricting access to data.


Making complex queries simple.
Ensuring data independence.
Providing different views of same data.

You might also like