SQL Questions
SQL Questions
Using Sum () Function with Over () Clause : This is the simplest method to calculate the cumulative
sum/running total in SQL Server. ...
Syntax:
SELECT country, registration_date,registred_users,
SUM(registred_users)
OVER (PARTITION BY country ORDER BY registration_date)
AS total_users
FROM registration;
England 2020-03-05 25 25
England 2020-03-06 12 37
England 2020-03-07 10 47
Poland 2020-03-05 32 32
Poland 2020-03-06 15 47
Poland 2020-03-07 6 53
ANS: Table Functions are used to implement the solutions where we need to return the results
represented in a table. Mostly we will implement table functions to address the data modelling
requirements which cannot be achieved using graphical calculation views.
▪ Accept multiple input parameters and return exactly one results table
Key Points:
▪ We can call the table functions from stored procedures and other table functions
Stored procedures are the reusable processing blocks of logic, which can be used to implement solutions
as per specific business requirements.
scenarios such as: 1) Persisting results in HANA database – example Snapshots, Reusable results of
HANA views – to avoid frequent execution of complex views.
▪ Scheduling the procedure call from XS Job engine which is in-build in HANA
▪ Scheduling the procedure call from external ETL tools such as Business Objects Data Services
Procedures can be created as:
1. Catalog Procedures: These are not transportable, since they are created using the CREATE
PROCEDURE statement and they are not created under a package.
2. Repository Procedures: These are the procedures created using the HANA development perspective
(Extension .hdbprocedure). This is the recommended approach for creating stored procedures since they
can be transported, and version management is available
Q3.Windows functions in SQL?
ANS: Windows functions perform a calculation across a set of table rows that are somehow related to
the current row.
Q5. What is the difference between the RANK() and DENSE_RANK() functions?
ANS: 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.
Q6. What is SQL example?
ANS: SQL is a database query language that allows you to edit, remove, and request data from databases. The
following statements are a few examples of SQL statements:
SELECT
INSERT
UPDATE
DELETE
CREATE DATABASE
ALTER DATABASE
ANS:
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:
ANS:
A stored procedure is a piece of prepared SQL code that you can save and reuse again and over.
So, if you have a SQL query that you create frequently, save it as a stored procedure and then call it to
run it.
You may also supply parameters to a stored procedure so that it can act based on the value(s) of the
parameter(s) given.
Stored Procedure Syntax
AS
sql_statement
GO;
EXEC procedure_name;
OR
A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over
again. So, if we have a SQL query that we create frequently, save it as a stored procedure and then call
it to run it.
We can use select, insert, update , delete etc in stored procedure.
We can not use stored procedure in Graphical CV directly for that we need to convert as scripted
calculated column and use it.
Scenarios: Use it to load in a Temporary table, to create hierarchies and inventory snapshot creation.
AS
sql_statement
GO;
EXEC procedure_name;
Inner join: Inner Join in SQL is the most common type of join. It is used to return all the rows from
multiple tables where the join condition is satisfied.
Left Join: Left Join in SQL is used to return all the rows from the left table but only the matching rows
from the right table where the join condition is fulfilled.
Right Join: Right Join in SQL is used to return all the rows from the right table but only the matching
rows from the left table where the join condition is fulfilled.
Full Join: Full join returns all the records when there is a match in any of the tables. Therefore, it
returns all the rows from the left-hand side table and all the rows from the right-hand side table.
ANS: Both Char and Varchar2 are used for characters datatype but varchar2 is used for character strings of
variable length whereas Char is used for strings of fixed length. For example, char(10) can only store 10
characters and will not be able to store a string of any other length whereas varchar2(10) can store any length i.e
6,8,2 in this variable.
ANS:
DELETE vs TRUNCATE
DELETE TRUNCATE
Delete command is used to delete a row in a table. Truncate is used to delete all the rows from a table.
You can rollback data after using delete statement. You cannot rollback data.
It is slower than truncate statement. It is faster.
ANS:
DROP command removes a table and it cannot be rolled back from the database whereas TRUNCATE
command removes all the rows from the table.
In SQL, the DROP command is used to remove the whole database or table indexes, data, and more. The
important part of this command is that it has the ability to permanently remove the table and its contents.
In SQL, the TRUNCATE command is used to remove all the rows from the table. However, the structure of
the table and columns remains the same. It is faster than the DROP command.
ANS: 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:
Q14. Write a SQL query to find the names of employees that begin with ‘A’?
ANS: To display name of the employees that begin with ‘A’, type in the below command:
Q15. Write a SQL query to get the third-highest salary of an employee from employee_table?
ANS:
ANS: 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.
Q19. What is the difference between ‘HAVING’ CLAUSE and a ‘WHERE’ CLAUSE?
ANS: HAVING clause can be used only with SELECT statement. It is usually used in a GROUP BY clause
and whenever GROUP BY is not used, HAVING behaves like a WHERE clause.
Having Clause is only used with the GROUP BY function in a query whereas WHERE Clause is applied to each
row before they are a part of the GROUP BY function in a query.
ANS: ALIAS command in SQL is the name that can be given to any table or a column. This alias name can be
referred in WHERE clause to identify a particular table or a column.
For example-
Select deptno, String_agg(Empname, ‘/ /’ from Emp); Now all the columns will be changed to rows..
Q23. What is difference between scalar function and user-defined function in SQL?
ANS: User-defined Scalar Functions (SFs) return a single scalar data value of the type defined in the
RETURNS clause. User-defined table-valued functions (TVFs) return a table data type that can read from in
the same way as you would use a table:
1. SUM() 1. LCASE()
2. COUNT() 2. UCASE()
3. AVG() 3. LEN()
4. MIN() 4. MID()
5. MAX() 5. ROUND()
6. FIRST() 6. NOW()
7. LAST() 7. FORMAT()
Q27. What are the currency conversion functionalities supported by SAP HANA?
ANS: To use the CONVERT_CURRENCY function, the currency conversion tables TCURV, TCURX,
TCURN, TCURR, and TCURF must be available in the SAP HANA database.
ANS:
We can only use Select in table functions and also only input parameters.
We can use it as a table directly.
Scenarios: We can use for all window functions like Rank , Denserank, string agg .
We can not write sql statements in scalar functions. A new function will be created.
Function Description
GO
[Persontable]
ANS: Below is simple query to find the employee whose salary is highest.
select *from employee where salary=(select Max(salary) from employee);
We can nest the above query to find the second largest salary.
select *from employee
group by salary
order by salary desc limit 1,1;
SELECT Empname, MAX(salary) AS salary
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee);
ANS:
Executing the above queries will return the first and last day with date, month, year, time etc. For example, if its
any day in the month of feb 2021, the output will be …
FirstDate
2021-02-01 10:44:27.557
LastDate
2021-02-28 10:44:27.557
However, we want the First and Last day (in words). So, simply embed the two above queries inside
the DATENAME() function. Here it is.
USE School
FROM Students
As you can see, we use the AVG aggregate function to calculate the average age of all the students in the
StudentAge column. The output of the above script looks like this:
Take a look at the third row of the RunningAgeAverage column. It contains the average of values of the
1st to 3rd rows in the StudentAge column, i.e (14 + 12 + 13)/3 = 13.