You are on page 1of 7

1.

You are being you being assigned a task to move 5 million rows from one server to
another using T-SQL with a linked-server. What will you consider to avoid transaction
log fill up at destination server?

Will prefer to use SET ROWCOUNT and a while loop to commit data in batches.

2. In what sequence SQL statement are processed?

The clauses of the select are processed in the following sequence

1. FROM clause

2. WHERE clause

3. GROUP BY clause

4. HAVING clause

5. SELECT clause

6. ORDER BY clause

7. TOP clause

3. What is a correlated sub-query?

A correlated sub-query is a nested query that is linked to the outer query. For instance,
say I wanted to find all the employees who have not entered their time for the week. I
could query the Employee table to get their first and last name, but I need to look at the
Time Entry table to see if they’ve entered their time or not. I can’t do a straight join here
because I’m looking for the absence of time data, so I’ll do a correlated sub-query similar
to this:

SELECT FirstName, LastName


FROM EMPLOYEE e
WHERE NOT EXISTS (SELECT 1 FROM TimeEntry te
WHERE te.EmpID = e.EmpID
AND te.WeekID = 35)

Notice that the inner query relates to the outer query on the employee ID, thus making
it a correlated sub-query. The inner query will be evaluated once per outer query row.

4. What are the different SQL Server Versions you have worked on?
5. How can I verify that backups are occurring on a daily basis?

 Review the SQL Server error log for backup related entries.

 Query the msdb.dbo.backupset table for the backup related entries.

 Review the file system where the backups are issued to validate they exist.
6. When are we going to use truncate and delete?

 TRUNCATE is a DDL command, whereas DELETE is a DML command.

 We can’t execute a trigger in case of TRUNCATE whilst with DELETE, we can


accomplish a trigger.

 TRUNCATE is quicker than DELETE, for the reason that when we use DELETE to delete
the data, at that time it store the whole statistics in the rollback gap on or after
where we can get the data back after removal. In case of TRUNCATE, it will not store
data in rollback gap and will unswervingly rub it out. TRUNCATE do not recover the
deleted data.

 We can use any condition in WHERE clause using DELETE but it is not possible with
TRUNCATE.5.If a table is referenced by any foreign key constraints, then TRUNCATE
won’t work.

7. Find What is Wrong in this Query?


SELECT subject_code, AVG (marks) FROM students WHERE AVG(marks) > 75 GROUP BY
subject_code;

The WHERE clause cannot be used to restrict groups. Instead, the HAVING clause should
be used.

SELECT subject_code, AVG (marks)


FROM students
HAVING AVG(marks) > 75
GROUP BY subject_code;

8. If you are a SQL Developer, how can you delete duplicate records in a table with no
primary key?

Use the SET ROWCOUNT command. For instance,


if you have 2 duplicate rows, you would SET ROWCOUNT 1, execute DELETE command
and then SET ROWCOUNT 0.

9. What is the SQL CASE statement used for? Explain with an example?
It allows you to embed an if-else like clause in the SELECT clause.

SELECT Employee_Name, CASE Location


WHEN 'alex' THEN Bonus * 2
WHEN 'robin' THEN Bonus *, 5
ELSE Bonus
END
"New Bonus"
FROM Intellipaat_employee;

10. How to find second highest salary of an Employee?

There are many ways to find second highest salary of Employees in SQ. You can either
use SQL Join or Sub query to solve this problem.
Here is SQL query using Subquery :

Select MAX(Salary) from Intellipaat_emplyee WHERE Salary NOT IN ( select MAX(Salary)


from Intellipaat_employee.

11. Explain how to send email from SQL database.

SQL Server has a feature for sending mails. Stored procedures can also be used for sending
mail on demand. With SQL Server 2005, MAPI client is not needed for sending mails.
The following is the process for sending emails from database.

Make sure that the SQL Server Mail account is configured correctly and enable
Database Mail.

Write a script to send an e-mail. The following is the script.

USE [YourDB]
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'xyz@intellipaat.com; abc@intellipaat.com;pqr@intellipaat.com’
@body = ' A warm wish for your future endeavor',
@subject = 'This mail was sent using Database Mail' ;
GO

12. Is the following a valid SQL query? Why or why not?

SELECT TOP 5 YEAR(BillingDate) AS BillingYear, COUNT(*) AS NumberOfInvoices


FROM Invoices
WHERE CustomerId = 42
GROUP BY YEAR(BillingDate)
HAVING COUNT(*) > 1
ORDER BY BillingYear;

One might think that this SQL statement is invalid since it uses the alias BillingYear in the
ORDER BY phrase. Even though BillingYear cannot be used in the WHERE or GROUP BY
phrases, it can be used in the ORDER BY phrase.

Listing the phrases in the logical order in which they are processed helps make clear why
this is so:

FROM Invoices
WHERE CustomerId = 42
GROUP BY YEAR(BillingDate)
HAVING COUNT(*) > 1
SELECT YEAR(BillingDate) AS BillingYear, COUNT(*) AS NumberOfInvoices
ORDER BY BillingYear
TOP 5

13. Can records be deleted from a View in SQL Server?

It depends.

There are two types of Views in SQL Server. One is a “simple” view that contains data
from one table only, and the other is a “complex” view that contains data from multiple
tables.

A delete operation can be performed on records in a simple view, but not in a complex
view.

14. What would be the SQL Server command to get the position of the letter ‘o’ in the
name ‘John’ from an Employee table

Select CHARINDEX('o',FIRST_NAME,0) from employee where first_name='John'

CHARINDEX is the SQL Server Equivalent of INSTR in Oracle.

15. What does UNION do? What is the difference between UNION and UNION ALL?
UNION merges the contents of two structurally-compatible tables into a single combined
table. The difference between UNION and UNION ALL is that UNION will omit duplicate
records whereas UNION ALL will include duplicate records.

It is important to note that the performance of UNION ALL will typically be better than
UNION, since UNION requires the server to do the additional work of removing any
duplicates. So, in cases where is is certain that there will not be any duplicates, or where
having duplicates is not a problem, use of UNION ALL would be recommended for
performance reasons.

16. What will be the result of the query below? Explain your answer and provide a version
that behaves correctly.
select case when null = null then 'Yup' else 'Nope' end as Result;

This query will actually yield “Nope”, seeming to imply that null is not equal to itself! The
reason for this is that the proper way to compare a value to null in SQL is with the is
operator, not with =.

Accordingly, the correct version of the above query that yields the expected result (i.e.,
“Yup”) would be as follows:

select case when null is null then 'Yup' else 'Nope' end as Result;

This is because null represents an unknown value. If you don’t know the value, you can’t
know whether it equals another value, so = null is always assumed to be false.

17. Assume a schema of Emp ( Id, Name, DeptId ) , Dept ( Id, Name).

If there are 10 records in the Emp table and 5 records in the Dept table, how many
rows will be displayed in the result of the following SQL query:

Select * From Emp, Dept

Explain your answer.

The query will result in 50 rows as a “cartesian product” or “cross join”, which is the default
whenever the ‘where’ clause is omitted.
18. How can you select all the even number records from a table? All the odd number records?

To select all the even number records from a table:

Select * from table where id % 2 = 0

To select all the odd number records from a table:

Select * from table where id % 2 != 0

19. What is the difference between the WHERE and HAVING clauses?

When GROUP BY is not used, the WHERE and HAVING clauses are essentially equivalent.

However, when GROUP BY is used:

 The WHERE clause is used to filter records from a result. The filtering occurs
before any groupings are made.

 The HAVING clause is used to filter values from a group (i.e., to check conditions
after aggregation into groups has been performed).

20. Given a table Employee having columns empName and empId, what will be the result
of the SQL query below?

select empName from Employee order by 2 desc;

“Order by 2” is only valid when there are at least two columns being used in select statement.
However, in this query, even though the Employee table has 2 columns, the query is only
selecting 1 column name, so “Order by 2” will cause the statement to throw an error while
executing the above sql query.

You might also like