BigRoz Big Roz
Home / Technology / SQL / SQL queries interview questions and answers

SQL queries interview questions and answers

Here I will going to explain some basic SQL queries interview questions and answers. What are different queries used in SQL Server with examples. It’s consist of Advanced Select Clause, SQL query to join multiple table and Filtering and Sorting SQL queries. In previous article I explained SQL Query join table, group by clause here I will focus on only 10 basic SQL queries interview questions and answers.

SQL queries interview questions and answers
SQL queries interview questions and answers

Description:

To read related to SQL queries interview questions and answers please go to SQL Server Section. Here I will focus on SQL queries interview questions and answers.

Advanced SQL Select Clause consist of Distinct, alias, scalar function and Case statement. SQL queries interview questions and answers solve using Adventure Works Database 2008.

Database reference – Adventure Works Database 2008 Free Download

List of SQL queries interview questions and answers

 

You tube link for SQL queries interview questions and answers

SQL queries interview questions and answers – 1) How to select unique rows in SQL server 2008?

Understanding Distinct Select

Distinct Clause is used to find only unique rows in result set.Remove Duplicate rows from SQL server database. Provide uniqueness across set of selected column.

SQL queries Syntax (Distinct Clause)

Select distinct <column> from table name or View

SQL queries interview questions and answers

SELECT distinct [PRODUCT NAME]

FROM PRODUCT

SQL queries interview questions and answers – How to change column name in SQL result?

Using Alias to mention column

You can alias column using various ways

  • Alias column using As
  • Alias column using =
  • Accidentally alias column

 

Here I will process in all three SQL queries examples for how to alias column name in SQL

SQL queries example Alias column using AS

SELECT   [Product Id], [Product Name], [Slandered Cost] as Cost  FROM PRODUCT

SQL queries example Alias column using =

SELECT   [Product Id], [Product Name], [Slandered Cost]= Cost   FROM PRODUCT

SQL queries example accidentally alias column

SELECT   [Product Id], [Product Name], [Slandered Cost] [List Price] FROM PRODUCT

 

SQL queries interview questions and answers – How to rename a table in SQL server 2008?

We can Alias to SQL table

  • Alias table using As
  • Alias table without using As
  • Using Alias table name in select clause

Here I will process in all three SQL queries examples with answers for how to alias table name in SQL

Create table alias in FROM clause using AS

SELECT   * FROM [Unit Measure] as Unit

Create table alias in FROM clause without using AS

SELECT   * FROM [Unit Measure] Unit

Create table Alias in select clause

SELECT   unit. Name FROM [Unit Measure] Unit

 

SQL queries interview questions and answers – What is use of case in SQL server 2008?

Here I will explain case SQL queries examples.

Simple Case Statement:

Simple case always compare sleeted value with available list of value and produce first match. If no match found then it goes to else clause and if no else then it returns null value.

Search case Statement:

Calculate set of expression, Check all possible logical expression and then perform case part like simple case statement. Means it start with then part and not found then go to else part.

Syntax:

Select <column>, Case when Column1 <logical expression >

then Column1<Result>

Else Column1 <Result>

End As Alias

From Table

Where OR Having

Order by

Case Expression Format

Keyword Expression
Select <select List >
Case <Value To Compare >
When <Value To Match >
Then <result>
End N/A
From <table source>

 

SQL queries interview questions and answers – How to use if else in SQL server 2008?

Select [Product Id], [product name,

CASE [product subcategory]

WHEN ‘1’

then ‘Machine’

ELSE ‘Tool’

END

From Product

 

SQL queries interview questions and answers – How to sort records in SQL server 2008?

Order By query sort result for presentation purpose. Order BY clause always used at the end so Order by clause is last clause to be logically processed .It is used to sort all null together. Using Order by clause we can sort result in ascending or descending manner for that use ASC, DESC respectively.

 SQL queries examples Order by Clause

Select [Product Id], [product name]

From Product

Order by [Product Name]

SQL queries interview questions and answers – How to sort result ascending order in SQL server 2008?

 SQL queries examples with answers Order by ascending order.

Select [Product Id], [product name]

From Product

Order by [Product Name] ASC

SQL queries interview questions and answers – How to sort result descending order in SQL server 2008?

SQL queries examples with answers Order by descending order.

Select [Product Id], [product name]

From Product

Order by [Product Name] DESC

 

SQL queries interview questions and answers – How to filter data in SQL server 2008?

SQL queries examples – How to filter data in SQL server 2008?

  1. Filter data in where Clause

Where Clause used to filter data in SQL .Must be used with Logical expression. Only condition satisfied rows are accepted .Where clause always used after FROM Clause and before Group by Clause

Syntax for where clause

Select [Product Id], [product name]

From Product

Where [Product Id] < 10

  1. Filter data in select Clause

Top allow you to number of rows or Percentage of rows to be return .Top and combination of Order By clause used to filter and display required data.

Syntax for Top

Select top (N) | top (N) % with column names

From table name

Order by column name ASC

SQL queries examples with answers select last 10 records

Select top (10) [Product Id], [product name]

From Product

Order by [Product Name] DESC

SQL queries interview questions and answers – How to handle NULL in SQL server 2012?

Different component of SQL server handle null differently. SQL server Filter null records using Where, Having clause .Order BY and Distinct Clause treat Null as component. To filter null use IS NULL or IS Not NULL rather than = NULL or <> NULL.

Example

Select [Product Id], [product name]

From Product

Where [Product name] is null

 


In my next article I will explain SQL Server Data Types. Please give me feedback on SQL queries interview questions and answers.

 

 

 

 

 

 

 
 

Author: Technicgang

“Technicgang Started on 2015” Rahul Samant is the founder & CEO of Technicgang. A blog based on science and technology. Techicgang receives 10K subscribers with in year.

About Technicgang

"Technicgang Started on 2015" Rahul Samant is the founder & CEO of Technicgang. A blog based on science and technology. Techicgang receives 10K subscribers with in year.

Check Also

RDBMS

Relational database management system examples

Here I will go to explain relational database management system examples. It includes RDMS concept, …

Leave a Reply

Be the First to Comment!

Notify of
wpDiscuz