Ms SQL server technical questions
Ms SQL server technical questions
SQL Fundamentals:
Q1: What are the basic components of an SQL
query?
A1: The basic components of an SQL query include
the SELECT clause, the FROM clause, and optional
clauses like WHERE, GROUP BY, HAVING, and
ORDER BY.
Q2: How do you use the DISTINCT keyword in a
query, and what does it do?
A2: The DISTINCT keyword is used in the SELECT
clause to retrieve only unique rows from the
result set. It eliminates duplicate rows.
Transact-SQL (T-SQL):
Q21: How do you declare and use variables in
08-07, 11:26
T-SQL?
A21: Use the DECLARE keyword to declare a
variable, then SET or SELECT to assign a value.
Example: DECLARE @count INT; SET @count = 10;
Q22: What are the differences between
user-defined functions and stored procedures in
T-SQL?
A22: Functions return a single value, while
procedures can return multiple output
parameters. Functions can be used in SELECT
queries, while procedures are executed using the
EXEC statement.
function in T-SQL.
A24: The ROW_NUMBER() function assigns a
unique number to each row within a result set.
Example: SELECT product_name,
ROW_NUMBER() OVER (ORDER BY
product_name) AS row_num FROM products;
transaction.
SQL Fundamentals:
Q56: What is the purpose of the COALESCE and
NULLIF functions in SQL, and how are they used?
A56: The COALESCE function returns the first
non-null expression from a list of expressions,
while the NULLIF function returns NULL if two
expressions are equal. They are helpful in
handling NULL values in queries.
Transact-SQL (T-SQL):
Q59: How do you use the OFFSET FETCH clause in
T-SQL, and what is its purpose?
A59: The OFFSET FETCH clause is used for
pagination to skip a specified number of rows and
return a limited number of rows from a result set.
It is often used for implementing paging in web
applications.
SQL Fundamentals:
Q66: What is the purpose of the ORDER BY clause
in SQL, and how do you use it to sort query
results?
A66: The ORDER BY clause is used to sort the
result set based on one or more columns in
ascending or descending order. Example: SELECT
column1, column2 FROM table_name ORDER BY
column1 ASC, column2 DESC;
08-07, 11:26
Transact-SQL (T-SQL):
Q69: How do you use the TRY...CATCH block in
08-07, 11:26
necessary considerations.
A74: To restore a database to a different server,
backup the database on the source server, copy
the backup file to the destination server, and
restore the database on the destination server.
Considerations include ensuring compatible SQL
Server versions and dealing with potential login
and file path differences.
SQL Fundamentals:
Q76: What is the difference between the WHERE
and HAVING clauses in SQL, and when would you
use each?
A76: The WHERE clause filters rows before
aggregation, while the HAVING clause filters
aggregated results. Use WHERE for row-level
filtering and HAVING for group-level filtering.
Transact-SQL (T-SQL):
Q79: How do you use the STRING_AGG() function
in T-SQL, and what is its purpose?
A79: The STRING_AGG() function concatenates
strings from multiple rows into a single string with
a specified separator.
SQL Fundamentals:
Q86: What is the purpose of the ALL and ANY/
SOME operators in SQL, and how are they used in
queries?
A86: The ALL operator compares a value with all
values returned by a subquery, and the ANY/
SOME operator compares a value with at least
one value returned by a subquery. They are used
with comparison operators in subqueries.
Transact-SQL (T-SQL):
Q89: How do you use the LAG() and LEAD()
window functions in T-SQL, and what do they do?
A89: The LAG() function retrieves the value from
a previous row, and the LEAD() function retrieves
the value from the next row, within the same
result set based on a specified ordering.
08-07, 11:26
SQL Fundamentals:
Q96: What are the differences between a primary
key and a unique key in SQL, and how do they
enforce data integrity?
A96: Both primary key and unique key enforce
08-07, 11:26
Transact-SQL (T-SQL):
08-07, 11:26