Showing posts with label SQL Interview Q/A. Show all posts
Showing posts with label SQL Interview Q/A. Show all posts

List of 90 Sql Server Interview Question and Answer

List of 90 SQL Server Interview Questions and Answers





Q #1) What is SQL?


Answer: Structured Query Language SQL is a database tool that is used to create and access the database to support software applications.


Q #2) What are tables in SQL?


Answer: The table is a collection of records and information in a single view.


Q #3) What are the different types of statements supported by SQL?


Answer:


statements supported by SQL


There are 3 types of SQL statements:


a) DDL (Data Definition Language): It is used to define the database structure such as tables. It includes three statements such as CREATE, ALTER, and DROP.


Some of the DDL Commands are listed below:


CREATE: It is used for creating the table.


CREATE TABLE table_name

column_name1 data_type(size),

column_name2 data_type(size),

column_name3 data_type(size),

ALTER: The ALTER table is used for modifying the existing table object in the database.


ALTER TABLE table_name

 ADD column_name datatype

OR


ALTER TABLE table_name

DROP COLUMN column_name


b) DML (Data Manipulation Language): These statements are used to manipulate the data in records. Commonly used DML statements are INSERT, UPDATE, and DELETE.


The SELECT statement is used as a partial DML statement, used to select all or relevant records in the table.


c) DCL (Data Control Language): These statements are used to set privileges such as GRANT and REVOKE database access permission to the specific user.


Q #4) How do we use the DISTINCT statement? What is its use?


Answer: The DISTINCT statement is used with the SELECT statement. If the record contains duplicate values then the DISTINCT statement is used to select different values among duplicate records.


Syntax:


SELECT DISTINCT column_name(s)

 FROM table_name;


Q #5) What are different Clauses used in SQL?


Answer:


Clauses used in SQL


WHERE Clause: This clause is used to define the condition, extract and display only those records which fulfill the given condition.


Syntax:


SELECT column_name(s)

 FROM table_name

 WHERE condition;


GROUP BY Clause: It is used with a SELECT statement to group the result of the executed query using the value specified in it. It matches the value with the column name in tables and groups the end result accordingly.


Syntax:


SELECT column_name(s)

 FROM table_name

 GROUP BY column_name;


HAVING clause: This clause is used in association with the GROUP BY clause. It is applied to each group of results or the entire result as a single group. It is much similar to the WHERE clause but the only difference is you cannot use it without the GROUP BY clause


Syntax:


SELECT column_name(s)

 FROM table_name

 GROUP BY column_name

 HAVING condition;


ORDER BY clause: This clause is used to define the order of the query output either in ascending (ASC) or descending (DESC). Ascending (ASC) is set as the default one but descending (DESC) is set explicitly.


Syntax:


SELECT column_name(s)

 FROM table_name

 WHERE condition

 ORDER BY column_name ASC|DESC;


USING clause: USING clause comes in use while working with SQL JOIN. It is used to check equality based on columns when tables are joined. It can be used instead of the ON clause in JOIN.


Syntax:


SELECT column_name(s)

 FROM table_name

 JOIN table_name

 USING (column_name);

How can I run a query in SQL?

How can I run a query in SQL?

Connecting to Your Database
Follow the procedure below to connect to your Vijay database.
1. Log in to a computer where SQL Server Management Studio is installed.
2. Launch SQL Management Studio from the Start menu.
3. Choose Database Engine as the Server type, choose the correct Server.
https://sqlsensationforu.blogspot.com/
Step 1 Connecting to Your Database

 Name from the list or browse for the correct server.
If your user account has the rights to access the database, leave Authentication as Windows Authentication. Otherwise, switch it to SQL Server Authentication and enter the User name and Password for a SQL account with rights to access the database. Note: If you are not sure which account to use, you can confirm what’s being used by Vijay by opening the Vijay Control Panel.
4. Click Connect.
Running a Query
1)In the Object Explorer pane, expand the top-level Server node and then Databases.
https://sqlsensationforu.blogspot.com/
Step 2 Select database
 2)Right-click your vCommander database and choose New Query.

3)Copy your query into the new query pane that opens.
https://sqlsensationforu.blogspot.com/
Step 3 Select query

4)Click Execute
https://sqlsensationforu.blogspot.com/
Step 4 Execute Query



What is the use of a schema in SQL Server?

What is the use of a schema in SQL Server?

Advantage of Schema:-

  1. Act as object protection tool: A schema can be a very effective object projection tool combined with the appropriate level of user permissions. 
  2. A DBA can maintain control access to an object that would be very crucial.
  3. Easy to maintain the database.
  4. A single schema can be shared among multiple databases and database users.
  5. A database user can be dropped without dropping database objects.
  6. Manipulation of and access to the object is now very complex and more secure. The schema acts as an additional layer of security.
  7. Database objects can be moved among schemas.
  8. The ownership of schemas is transferable

How do I display any string in SQL with a select query?

How do I Show any string in SQL with a pick query?


You can write a query in the double quotation.

Example:-

Select “ For additional concerning sql server visit SQL World here”

Output:-


For more about sql ser visit SQL World here

What does * mean in SQL?

What does * mean in SQL?


In Sql server * means it returns all the columns from the table.

Example:-

Let assume you have an Employee table and in this table, you have 1000 records. So, If you write query “SELECT * FROM EMPLOYEE “


It will return all the columns from the EMPLOYEE table.

What is the use of the MSDB database in SQL Server?


What is the use of the MSDB database in SQL Server?
The msdb database is used mainly by the SQL Server Agent to store system activities like sql server jobs, mail, service broker, maintenance plans, user and system database backup history, etc..It is also used by the database engine and management studio.

How can we write user-defined functions (UDF) in my SQL server?

----------------------------
How can we write user-defined functions (UDF) in my SQL server?

There are three types of user-defined functions(UDF) in SQL server
1)Scalar function
2)Inline function
3)Multi statement Table-valued function


1)Scalar function:-
->Scalar UDFs return a single value.
->Scalar UDFs built-in functions such as  GETDATE(),or OBJECT_NAME(), which returns single value,date or integer. The value returned by a Scalar UDF can be based on the parameters passed

Syntax:-

Create Functions  function-name(Parameters)
Returns returns-type
AS
BEGIN
      Statements 1
      Statements 2
      Statements 3
       .
       .
       Statements n
END

Example:-

Create Functions  GetEmployee(@DepartmentID int)
Returns varchar(50)
AS
BEGIN
     RETURN (SELECT Name FROM Employee WHERE       DepartmentID=@DepartmentID )
     END


2)Inline function:-
->Inline UDFs return single rows or multiple rows and can contain a single SELECT statement. Because in-line UDF is limited to a single SELECT statement.

Syntax:-

Create Functions  function-name(Parameters)
Returns returns-type
AS
BEGIN
     RETURN


Example:-

Create Functions  GetEmployee(@DepartmentID int)
Returns varchar(50)
AS
BEGIN
     RETURN 
SELECT Name FROM Employee WHERE  DepartmentID=@DepartmentID 

3)Multi statement Table-valued function:-

->Multi-statements UDF can contain any number of statements that populated the table variable to be returned.


Syntax:-

Create Functions  function-name(Parameters)
Returns @TableName TABLE
(
Column_1  Datatype,
Column_2  Datatype,
.
Column_n  Datatype
)
AS
BEGIN
Statements 1
                  Statements 2
                  Statements 3
                  .
                  .
                 Statements n
                 RETURN
END

Example:-


Create Functions  GETaverage(@Name varchar(50))
Returns  @Marks TABLE
(
Name varchar(50),
Class varchar(50),
Average Decimal(4,2)
)
AS
BEGIN
DECLARE @Avg DECIMAL(4,2)
DECLARE @Rno INT 
INSERT INTO @Marks (Name)VALUES(@Name)
SELECT @Rno=Rno FROM Student WHERE Name=@Name 
SELECT @Avg=(Subject1+Subject2+Subject3)/3 FROM Subjects WHERE Rno=@Rno

UPDATE @Marks SET
Subject1=(SELECT Subject1 FROM Subjects WHERE Rno=@Rno),
Subject2=(SELECT Subject2 FROM Subjects WHERE Rno=@Rno),
Subject3=(SELECT Subject3 FROM Subjects WHERE Rno=@Rno)
Average=@Avg
WHERE Name=@Name
                  
                 RETURN
END