Monday, October 3, 2022

SQL Interview Preparation

 

SQL Interview Q& A Part- 1


1.Explain what is partitioning, hash partitioning and round robin partitioning?
  • By informatica data is distributed evenly among all partitions
  • In each partition where the number of rows to process are approximately same this partioning is applicable
  • For the purpose of partitioning keys to group data among partitions Informatica server applies a hash function
  • It is used when ensuring the processes groups of rows with the same partitioning key in the same partition need to be ensured

2. What is the difference between DROP and TRUNCATE statements?

If a table is dropped, all things associated with the tables are dropped as well. This includes — the relationships defined on the table with other tables, the integrity checks and constraints, access privileges and other grants that the table has. To create and use the table again in its original form, all these relations, checks, constraints, privileges and relationships need to be redefined. However, if a table is truncated, none of the above problems exist and the table retains its original structure.

3. What is the difference between DELETE and TRUNCATE statements?

The TRUNCATE command is used to delete all the rows from the table and free the space containing the table.
The DELETE command deletes only the rows from the table based on the condition given in the where clause or deletes all the rows from t

4. What is an Index?

An index is performance tuning method of allowing faster retrieval of records from the table. An index creates an entry for each value and it will be faster to retrieve data.

  1. Clustered Index
  2. Non-Clustered Index
  3. Column Store Index
  4. Filtered Index
  5. Hash Index
  6. Unique Index

5. How to fetch alternate records from a table?

Records can be fetched for both Odd and Even row numbers -.

Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=0
Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=1

6. What are the different subsets of SQL?

  • Data Definition Language (DDL) — It allows you to perform various operations on the database such as CREATE, ALTER, and DELETE objects.
  • Data Manipulation Language(DML) — It allows you to access and manipulate data. It helps you to insert, update, delete and retrieve data from the database.
  • Data Control Language(DCL) — It allows you to control access to the database. Example — Grant, Revoke access permissions.

7. What is the usage of the NVL() function?

You may use the NVL function to replace null values with a default value. The function returns the value of the second parameter if the first parameter is null. If the first parameter is anything other than null, it is left alone.

8. 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.

9. Use COALESCE to return the first non-null expression

The SQL Coalesce is used to manage the NULL values of the database. In this method, the NULL values are substituted with the user-defined value. The SQL Coalesce function assesses the parameters in series and always delivers the first non-null value from the specified argument record.

Syntax

1

Output

ByteScout

10. DENSE_RANK()Analytical query

It is an analytic query that computes the rank of a row in an arranged collection of rows. An output rank is a number starting from 1. DENSE_RANK is one of the most important SQL queries. It returns rank preferences as sequential numbers. It does not jump rank in event of relations. For example, the following query will give the sequential ranks to the employee.

11.Query_partition_clause

The query_partition_clause breaks the output set into distributions, or collections, of data. The development of the analytic query is limited to the confines forced by these partitions, related to the process a GROUP BY clause modifies the performance of an aggregate function. If the query_partition_clause is eliminated, the entire output collection is interpreted as a separate partition.

12. Finding the last five records from the table

Now, if you want to fetch the last eight records from the table then it is always difficult to get such data if your table contains huge information. For example, you want to get the last 8 records from the employee table then you can use rownum and a union clause. The rownum is temporary in SQL.

13. LAG

The LAG is applied to get data from a prior row. This is an analytical function. For example, the following query gives the salary from the prior row to compute the difference between the salary of the current row and that of the prior row. In this query, the ORDER BY of the LAG function is applied. The default is 1 if you do not define offset. The arbitrary default condition is given if the offset moves past the range of the window. The default is null if you do not define default.

Syntax

SELECT dtno,

Output

DTNO ENO ENAME JOB SAL SAL_PREV

14. LEAD

The LEAD is also an analytical query that is applied to get data from rows extra down the output set. The following query gives the salary from the next row to compute the deviation between the salary of the prevailing row and the subsequent row. The default is 1 if you do not define offset. The arbitrary default condition is given if the offset moves past the range of the window. The default is null if you do not define default.

15. WITH (Common Table Expressions)

A common table expression (CTE) is a defined short result set that endures within the range of a particular statement and that can be called later within that statement, perhaps on many occasions. The following query is describing the CTE:

Syntax

WITH all_emp

16.MIN

Utilizing a blank OVER clause converts the MIN into an analytic function. This is also an analytical query. In this, the entire result set is interpreted as a single partition. It gives you the minimum salary for all employees and their original data. For example, the following query is displaying the use of MIN in the Select query.

17. MAX

Using a blank row OVER clause converts the MAX into an analytic function. The lack of a partitioning clause indicates the entire output set is interpreted as a separate partition. This gives the maximum salary for all employees and their original data. For example, the following query displays the use of MAX in the select query.

18. SQL Query Order of Execution

In SQL, the order of written code differs from the order of execution. So in order to understand how the SQL query works you’ll need to understand proper SQL query structure and the order in which SQL queries must be written using following keywords

19.What is the difference between UNION and JOIN?

A JOIN allows a user to find records on another table based on a given condition between the two tables.

20. What is the difference between UNION and UNION ALL?

UNION ALL is almost the same at UNION except it will return all rows of both datasets, even if there are duplicates.

21.How to write the sql query for below scenario?

I/P:Mohit

22. How to display following query?

  • *
  • **
  • ***

23. How to display 1 to 100 number using query?

select level from dual connected by level ≤100

24. How to fetch the 3rd highest salary?

select * from (select dense_rank() over (order by sal) as rnak ,salary from emp) where rnak=3;

25. What is pivot table?

1. Pivot:

SELECT (ColumnNames) 
FROM (TableName)
PIVOT
(
AggregateFunction(ColumnToBeAggregated)
FOR PivotColumn IN (PivotColumnValues)
) AS (Alias) //Alias is a temporary name for a table
SELECT (ColumnNames) 
FROM (TableName)
UNPIVOT
(
AggregateFunction(ColumnToBeAggregated)
FOR PivotColumn IN (PivotColumnValues)
) AS (Alias)
SELECT CourseName, CourseCategory, Price 
FROM
(
SELECT CourseName, PROGRAMMING, INTERVIEWPREPARATION FROM geeksforgeeks
PIVOT
(
SUM(Price) FOR CourseCategory IN (PROGRAMMING, INTERVIEWPREPARATION)
) AS PivotTable
) P
UNPIVOT
(
Price FOR CourseCategory IN (PROGRAMMING, INTERVIEWPREPARATION)
)
AS UnpivotTable

Spark- Window Function

  Window functions in Spark ================================================ -> Spark Window functions operate on a group of rows like pa...