Showing posts with label sql server tutorials. Show all posts
Showing posts with label sql server tutorials. Show all posts

Sunday, June 22, 2025

SQL SERVER TOP 50 INTERVIEW QUESTION AND ANSWERS

 # 50 SQL Server Interview Questions with Answers


## Basic SQL Server Questions


1. **What is SQL Server?**

   - SQL Server is a relational database management system (RDBMS) developed by Microsoft that supports transaction processing, business intelligence, and analytics applications.


2. **What are the different editions of SQL Server?**

   - Enterprise, Standard, Web, Developer, and Express editions.


3. **What is the difference between clustered and non-clustered indexes?**

   - A clustered index determines the physical order of data in a table (only one per table). A non-clustered index is a separate structure that points to the data (multiple allowed per table).


4. **What is a primary key?**

   - A primary key is a column or set of columns that uniquely identifies each row in a table and cannot contain NULL values.


5. **What is a foreign key?**

   - A foreign key is a column or set of columns that establishes a relationship between data in two tables, enforcing referential integrity.


## Intermediate SQL Server Questions


6. **What is the difference between DELETE, TRUNCATE, and DROP?**

   - DELETE removes rows one at a time with logging, TRUNCATE removes all rows quickly without logging individual row deletions, DROP removes the entire table structure.

Sunday, August 28, 2016

SQL Server Cursor Example

SQL Server Cursor Example:


In some circles cursors are never used, in others they are a last resort and in other groups they are used regularly.  In each of these camps they have different reasons for their stand on cursor usage.  Regardless of your stand on cursors they probably have a place in particular circumstances and not in others.  So it boils down to your understanding of the coding technique then your understanding of the problem at hand to make a decision on whether or not cursor based processing is appropriate or not.  To get started let's do the following:
  • Look at an example cursor
  • Break down the components of the cursor
  • Provide additional cursor examples
  • Analyze the pros and cons of cursor usage

Example SQL Server Cursor

Here is an example cursor from tip Simple script to backup all SQL Server databases where backups are issued in a serial manner:
DECLARE @name VARCHAR(50-- database name  DECLARE @path VARCHAR(256-- path for backup files  DECLARE @fileName VARCHAR(256-- filename for backup  DECLARE @fileDate VARCHAR(20-- used for file name 
SET @path 'C:\Backup\'  
SELECT @fileDate CONVERT(VARCHAR(20),GETDATE(),112
DECLARE db_cursor CURSOR FOR  
SELECT 
name FROM MASTER.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb')  
OPEN db_cursor   FETCH NEXT FROM db_cursor INTO @name   
WHILE @@FETCH_STATUS 0   BEGIN   
       SET 
@fileName @path @name '_' @fileDate '.BAK'  
       
BACKUP DATABASE @name TO DISK = @fileName  

       
FETCH NEXT FROM db_cursor INTO @name   END   

CLOSE 
db_cursor   DEALLOCATE db_cursor

SQL Server Cursor Components

Based on the example above, cursors include these components:
  • DECLARE statements - Declare variables used in the code block
  • SET\SELECT statements - Initialize the variables to a specific value
  • DECLARE CURSOR statement - Populate the cursor with values that will be evaluated
    • NOTE - There are an equal number of variables in the DECLARE CURSOR FOR statement as there are in the SELECT statement.  This could be 1 or many variables and associated columns.
  • OPEN statement - Open the cursor to begin data processing
  • FETCH NEXT statements - Assign the specific values from the cursor to the variables
    • NOTE - This logic is used for the initial population before the WHILE statement and then again during each loop in the process as a portion of the WHILE statement
  • WHILE statement - Condition to begin and continue data processing
  • BEGIN...END statement - Start and end of the code block
    • NOTE - Based on the data processing multiple BEGIN...END statements can be used
  • Data processing - In this example, this logic is to backup a database to a specific path and file name, but this could be just about any DML or administrative logic
  • CLOSE statement - Releases the current data and associated locks, but permits the cursor to be re-opened
  • DEALLOCATE statement - Destroys the cursor

Saturday, January 24, 2015

SQL SEVER INTERVIEW QUESTION


1. What is  the default "SORT" order for SQL SEVER?


2. select adresses which are between  " 1/1/2015" and "1/04/2015" ?

3.why should you use CTE rather than using simple views?


Thursday, February 16, 2012

What is Data Base Schema?


A database schema is a way to logically group objects such as tables, views, stored procedures etc. Think of a schema as a container of objects.
You can assign a user login permissions to a single schema so that the user can only access the objects they are authorized to access.
Schemas can be created and altered in a database, and users can be granted access to a schema. A schema can be owned by any user, and schema ownership is transferable.

Creating a Database Schema

Creating a database schema in SQL Server Management Studio
  1. Complete the details in the General tab for the new schema. In this example, the schema name is "person" and the schema owner is "Homer".
    Creating a database schema in SQL Server Management Studio
  2. Add users to the schema as required and set their permissions:
    Creating a database schema in SQL Server Management Studio
  3. Add any extended properties (via the Extended Properties tab)
  4. Click OK.

Add a Table to the New Schema

Now that we have a new schema, we can add objects such as tables, views, and stored procedures to it. For example, we could transfer the table that we created in the earlier lesson to the new schema.
When we created that table (called "Individual"), it was created in the default database schema ("dbo"). We know this because it appears in our object browser as "dbo.Individual".
To transfer the "Individual" table to the person "schema":
  1. In Object Explorer, right click on the table name and select "Design":
    Changing database schema for a table in SQL Server Management Studio
  2. From Design view, press F4 to display the Properties window.
  3. From the Properties window, change the schema to the desired schema:
    Changing database schema for a table in SQL Server Management Studio
  4. Close Design View by right clicking the tab and selecting "Close":
    Closing Design View
  5. Click "OK" when prompted to save
Your table has now been transferred to the "person" schema.

Confirm your Change

To confirm the change:
  1. Refresh the Object Browser view:
    Refreshing the view in Object Browser
  2. You will now see that Object Browser displays the new schema for the table (person.Individual):
    Screenshot of the table in Object Browser