1.Write the script to create Table.
CREATE TABLE Test
2. What is Primary Key Constraint?
The PRIMARY KEY constraint uniquely identifies each record in a database table. It must contain unique values and must not contain null values. Most of the tables should have the Primary key but there exists only one Primary Key for one table. Primary key does not contain null value. Primary Key has Unique Cluster Index by default.
- What is Foreign Key Constraint?
A FOREIGN KEY in one table points to a PRIMARY KEY in another table. There can exist multiple Foreign Key and Foreign Key may have null values. The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column because it has to be one of the values contained in the table it points to.
- What is Unique Constraint?
The UNIQUE constraint uniquely identifies each record in a database table. Unique Constraint and Primary Key both provides the uniqueness of data in database table. There can be multiple Unique Key in Table but only One Primary Key. Primary Key itself has automatically defined Unique Constraint on it. Unique key can contain maximum of one null value where as primary does not.
- How Union and Union All works in SQL server?
Union gives the record only once if it occurs several times but Union All gives the record as several time that occurs in the table. Means that Union gives the distinct record where as Union All allows duplicate.
e.g. Table 1
Union All Result
- What are SQL Join?
Inner Join: If we need Common records from both the tables we use Inner Join.
Outer Join: when we need the common and uncommon record from both the table outer join is used. An outer join is of two type on the basis of the unmatching record of which table is needed.
Left Outer Join: This gives all the record of the Left table and common record of the right table. Right Outer Join: This gives all the record of the Right table and common record of the left table.
Full Join: It gives all the record of both tables.
7. Suppose you have the script of one database for one client and you need to create the same database for another client for same project deployment. How you ensure there is an exact same database, there is no mismatch of the table, table name, columns and constraints in both the Database?
By checking the Number of Tables, Number of Columns, Number of Constraints, Table Name etc we can ensure that there is no mismatch of database creation if there is anything missed we can find out what is missing with the help of it.
Query for Number of Table Count
select COUNT(* ) from INFORMATION_SCHEMA.TABLES
Query for Number of Column count
select COUNT(* ) from INFORMATION_SCHEMA.COLUMNS
Query for Number of Constraint Count
select COUNT(* ) from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
If there is one table creation missing then running following below query in both database and comparing each other, we can find out which table is missing. This will give the name list of table in the database.
select TABLE_NAME from INFORMATION_SCHEMA.TABLES
- How many types of Function exists in SQL server explain with example?
There are two types of SQL Function
- SQL Aggregate Functions
It returns a single value, calculated from values in a column.
Some of the Aggregate SQL Function are
AVG() – Returns the average value
COUNT() – Returns the number of rows
FIRST() – Returns the first value
LAST() – Returns the last value
MAX() – Returns the largest value
MIN() – Returns the smallest value
SUM() – Returns the sum
2. SQL Scalar functions
SQL Scalar functions return a single value, based on the input value.
Some of the SQL Scalar Functions are
UCASE() – Converts a field to upper case
LCASE() – Converts a field to lower case
MID() – Extract characters from a text field
LEN() – Returns the length of a text field
ROUND() – Rounds a numeric field to the number of decimals specified
NOW() – Returns the current system date and time
FORMAT() – Formats how a field is to be displayed
- What is Difference Between Store Procedure and Function?
– A Stored Procedure can return zero, single or multiple values. But, a function must return a single value.
– A Stored Procedure can have both input and output parameters whereas Functions can have only input parameters.
– Functions allow only a SELECT command whereas a Stored Procedure allows SELECT and other DML commands (INSERT, UPDATE and DELETE).
– A Stored Procedure can call a function, but a function can’t call a Stored Procedure.
– A Stored Procedure can use the try-catch block to handle exceptions. But try-catch is not allowed in functions.
– SQL transactions can be used in Stored Procedures but not in Functions.
- What is SQL Aliases?
Aliases are used to give database Table Name or Table Column Name as Temporary Name. It is used to make column Name more readable.
SELECT ColumnName AS AliasName