What is a table in SQL Server?
A table in SQL Server is a database object that contains all the data in a database. It is organized in a row-and-column format similar to a spreadsheet.
What happens when you try to insert a row with a foreign key that doesn’t exist in the referenced table?
If you try to insert a row with a foreign key that doesn’t exist in the referenced table, you will receive an error. The database engine will enforce referential integrity and prevent the insertion of invalid data.
What is the purpose of the SELECT statement in SQL Server?
The SELECT statement is used to retrieve data from one or more tables in a database.
It allows you to specify the columns you want to retrieve, apply filters using WHERE clause, sort the result set using ORDER BY clause, and perform various other operations on the data
What is a function in SQL Server?
A function is a pre-built SQL statement that performs a specific task.
Functions can accept input parameters, perform actions, and then return the result.
Functions always return either a single value or a table
What is the maximum number of columns allowed in a standard user-defined table?
A standard user-defined table can have up to 1,024 columns.
What is the difference between a left join and a right join in SQL?
A left join returns all the rows from the left table and matching rows from the right table. If there are no matching rows in the right table, the result will contain null values for all columns of the right table.
A right join is similar to a left join, but it returns all the rows from the right table and matching rows from the left table. If there are no matching rows in the left table, the result will contain null values for all columns of the left table.
Can a table have multiple foreign keys?
Yes, a table can have multiple foreign keys. Each foreign key establishes a separate relationship with another table.
How can I retrieve only the top 10 rows from Customers table in SQL Server?
SELECT TOP 10 * FROM Customers;
What are the types of user-defined functions available in SQL Server?
The following types of user-defined functions are available in SQL Server:
How do you create a table in SQL Server?
To create a table in SQL Server, you can use the CREATE TABLE statement followed by the table name and column definitions.
What is normalization in database design, and why is it important?
Normalization is a process of organizing data in a database to reduce redundancy and improve data integrity.
It involves breaking down large tables into smaller, more manageable tables and establishing relationships between them.
Normalization helps to eliminate data inconsistencies and anomalies such as update anomalies, insertion anomalies, and deletion anomalies.
It also helps to improve query performance and reduce storage requirements.
What is an index in SQL Server?
An index is an on-disk structure associated with a table or view that speeds retrieval of rows from the table or view. An index contains keys built from one or more columns in the table or view. These keys are stored in a structure (B-tree) that enables SQL Server to find the row or rows associated with the key values quickly and efficiently
How can I sort the result set of a SELECT statement in SQL Server?
To sort the result set of a SELECT statement, you can use the ORDER BY clause.
The ORDER BY clause allows you to specify one or more columns by which you want to sort the result set. For example, to sort the result set of a query by the “LastName” column in ascending order, you can use the following query:
SELECT * FROM Customers ORDER BY LastName ASC;
How do you create a user-defined function in SQL Server?
o create a user-defined function in SQL Server, you can use the CREATE FUNCTION statement.
Here’s an example that demonstrates how to create a scalar function named “GetFullName” that concatenates the first name and last name of an employee:
CREATE FUNCTION GetFullName (@FirstName VARCHAR(50), @LastName VARCHAR(50))
RETURNS VARCHAR(100)
AS
BEGIN
RETURN @FirstName + ' ' + @LastName
END
How is data organized in a table?
How is a foreign key different from a primary key?
A primary key uniquely identifies each record in a table and ensures that it has a unique value. A foreign key, on the other hand, establishes a relationship between two tables by referencing the primary key or a unique key of another table.
What are the types of indexes available in SQL Server?
The following types of indexes are available in SQL Server:
How can I group rows in SQL Server based on a specific column?
To group rows in SQL Server based on a specific column, you can use the GROUP BY clause.
The GROUP BY clause allows you to group rows that have the same values in one or more columns.
SELECT CustomerID, COUNT(*) FROM Orders GROUP BY CustomerID;
What is a stored procedure in SQL Server? and its advantages
A stored procedure is a precompiled set of SQL statements that perform a specific task. Stored procedures can accept input parameters, perform actions, and then return the result.
Stored procedures are stored in the database and can be called from an application or another stored procedure
Stored procedures offer several advantages over ad-hoc SQL statements:
Create a table called Employees with columns for EmployeeID, LastName, FirstName, Title, and HireDate
CREATE TABLE Employees (
EmployeeID int,
LastName varchar(255),
FirstName varchar(255),
Title varchar(255),
HireDate date
);
What is referential integrity, and how does it relate to foreign keys?
Referential integrity ensures that relationships between tables are maintained. It prevents actions that would violate these relationships, such as deleting a record that is referenced by another table’s foreign key. Foreign keys play a crucial role in enforcing referential integrity.
What is a view in SQL Server?
A view is a virtual table whose contents are defined by a query.
It consists of a set of named columns and rows of data. A view does not exist as a stored set of data values in a database.
Instead, the rows and columns of data come from tables referenced in the query defining the view and are produced dynamically when the view is referenced.
A view acts as a filter on the underlying tables referenced in the view.
The query that defines the view can be from one or more tables or from other views in the current or other databases
. How can you use string functions (LEN, CONCAT ,UPPER)and SUM function in a SELECT statement?
You can use various string functions and data functions in a SELECT statement to manipulate and transform data.
Some commonly used string functions include LEN, SUBSTRING, LOWER, UPPER, and CONCAT. Some commonly used data functions include SUM, AVG, COUNT, and MAX.
Here’s an example that demonstrates how to use these functions:
SELECT LEN(FirstName), UPPER(LastName), CONCAT(FirstName, ' ', LastName), SUM(OrderTotal)
FROM Customers
GROUP BY LEN(FirstName), UPPER(LastName), CONCAT(FirstName, ' ', LastName);
What is a parameterized stored procedure in SQL Server?
Given example for getting list of customers who is leaving in Hyderabad using stored procedure ?
parameterized stored procedure is a stored procedure that accepts one or more input parameters.
Input parameters allow you to pass values into the stored procedure at runtime,
Here is the definition of SP to get customer by city
CREATE PROCEDURE GetCustomersByCity
@City VARCHAR(50)
AS
BEGIN
SELECT * FROM Customers WHERE City = @City;
END
Execute SP
Declare @City as varchar(10)='Hyderabad;
Execute GetCustomersByCity @City