Learn T-SQL

SQL Basics?                                            SQL Server Interview Questions And Answers
 
 

SQL TOP
SQL LIKE
SQL Wildcards
SQL IN
SQL BETWEEN
SQL Alias
SQL UNION
SQL UNION ALL
SQL SELECT INTO
SQL CONSTRAINTS
Data Definition Language (DDL)
Data Manipulation Language (DML)
SQL Server Data Types
SQL NULLs
SQL ISNULL
SQL NULLIF
SQL Date and Time Functions
T-SQL Control of Flow
CURSOR
T-SQL Operators
PRINT
RAISERROR
Error Handling
SET Statements
Security Statements
BACKUP
RESTORE
TRANSACTIONS
 
 
 
 
 
 
 
 
 
 

SQL Server Interview Questions And Answers

How to Create Database using T-SQL command?

Explain Candidate Key, Alternate Key, and Composite Key

How to create Primary Key on existing table?

How to create Foreign Key on existing table?

How to create FOREIGN KEY constraint by using WITH NOCHECK

How to add a DEFAULT constraint to an existing column?

How to add CHECK CONSTRAINTS on existing table?

What's the difference between a primary key and a unique key constraints?

Explain SQL Server JOINs with examples?

What is the difference between TRUNCATE and DELETE?

Write T-SQL query to calculate Month End Date

How to get Month Number from Month Name

Write T-SQL Query to find Nth largest Number

Maximum Capacity Specifications for Database Objects in SQL Server 2008 R2

What is View in SQL Server?

What is Stored Procedure?

What is User Defined Functions (UDF)

What is User-Defined Function (UDF) in SQL Server?

Types of User Defined Functions (UDF) in SQL Server?

Difference between Function and Stored Procedure?

What is the difference between a local and a global temporary tables?

What is a NOLOCK?

What is lock escalation?

What is Log Shipping in SQL Server?

What is WITH TIES clause in SQL Server?

COUNT Number of Records for all the Tables in a Database?

Function to Split Multi-valued String

How to connect to SQL Server using RUNAS command from command prompt?

Regular Expression Problem in T-SQL

Concatenating Row Values using Transact-SQL

Which TCP/IP port does SQL Server run on? How can it be changed?

What is uniqueidentifier in SQL Server?

What is NEWSEQUENTIALID?

What is Trace Flag 610?

Sleep Command in T-SQL?

What is the use UPDATE_STATISTICS command?

How to Granting Execute permission on All the Stored Procedure?

How to get SQL Server Restore history using T-SQL?

Function to Convert Decimal Number into Binary, Ternary, and Octal

How to find all the IDENTITY columns in a database?

Difference between a WHERE clause and HAVING clause?

  • WHERE clause used to filter the physical rows in your table(s) whereas the HAVING clause operates on groups of a result-set (usually aggregate) and specifies which of the summary rows are returned in your resultset.  
  • A WHERE clause is independent of GROUP BY clause but the HAVING clause does depend on this, although it can be used without this as well.
  •  First SQL Server filters the physical rows using WHERE conditions and then performs GROUP BY on filtered data in memory and then filters the aggregated rows again with HAVING clause.

Example:
List of all the employee with basic greater than 3000.

Now I want to calculate the Dept codes for which total of basic is greater than 20,000 for those employees who are getting basic more than 3000.

T-SQL Clauses

Most commonly used T-SQL command is SELECT statement. Below are the basic T-SQL clauses every SQL beginner must know:

SELECT <Column_List>
FROM <Table_Name>
WHERE <Conditional_Statement>
GROUP BY <Column_List>
HAVING <Conditional_Statement>
ORDER BY <Column_List>


Lets take an example of Employee table to understand these clauses:

EmployeeID
FirstName
MiddleName
LastName
DOB
Basic
Department
1
Hari
N
Sharma
10/10/1980
2500
Sales
2
John
D
Howell
11/11/1981
1900
Sales
3
Vijay
NULL
Sharma
10/20/1982
1500
Engineering
4
Paul
NULL
Catlin
7/25/1976
2600
Engineering
5
Romil
F
Driscoll
1/13/1979
2400
Sales
6
Karthik
P
Nagarajan
8/10/1969
2800
Engineering
7
Hany
H
Rodriguez
4/10/1977
2200
Management
8
William
NULL
Green
1/21/1981
1800
Management
9
Michelle
NULL
Jones
5/19/1974
2300
Management
10
Thomas
D
Lee
3/31/1984
1600
Sales

SELECT statement is used to pull the data from a table, view, or table valued function.
FROM clause is used to specify the name of the source table which we pull the data from.

For instance, we want to pull FirstName. LastName of all the employees along with Department name then we will use following query:

SELECT  FirstName,LastName,Department
FROM    dbo.Employee

Here is output:


WHERE clause is used to filter the data coming from a table. For instance, you want to pull FirstName, MiddleName, LastName, and Basic of all employees who belong to Sales department:

SELECT  FirstName,MiddleName,LastName,Basic
FROM    dbo.Employee
WHERE   Department = 'Sales'

 
Here is output:






 

GROUP BY clause is used to aggregate the data based on certain group of columns. For instance, find out the TOTAL of basic amount for each department.

SELECT  Department, SUM(Basic) BasicTotal
FROM    dbo.Employee
GROUP BY Department

Here is output:





 

HAVING clause is used to filter out the data based on aggregated values. For instance, find out the list of Departments for which Total of basic is greater than 6500:

SELECT  Department, SUM(Basic) BasicTotal
FROM    dbo.Employee
GROUP BY Department
HAVING   SUM(Basic) > 6500

I will explain the difference between WHERE and HAVING in my next post.


ORDER BY clause is used to arrange the result set in desired order, We can order the result in DESC or ASC order. For instance, get the list of all the employees in the order of First Name and then Last Name:

SELECT  FirstName,MiddleName,LastName,Basic
FROM    dbo.Employee
ORDER BY FirstName,LastName

You can also write this query in following way (1 means first column in SELECT statement and 2 means second column the SELECT statement):

SELECT FirstName,MiddleName,LastName,Basic
FROM dbo.Employee
ORDER BY 1,2