Understanding data types and constraints in SQL Server is crucial for designing robust and efficient databases. Familiarity with SQL Server data types, key constraints, and integrity rules will help you excel in interviews. Below are 30 key interview questions and answers, organized by topics, to ensure you’re well-prepared for your SQL Server interview.
INT
, DECIMAL
), string types (e.g., VARCHAR
, CHAR
), date/time types (e.g., DATE
, DATETIME
), and more.VARCHAR
and CHAR
in SQL Server?
VARCHAR
is a variable-length string data type, while CHAR
is a fixed-length string type. VARCHAR
uses only as much space as needed, while CHAR
always uses the specified length.DATETIME
and DATE
data types in SQL Server.
DATETIME
stores both date and time values, while DATE
only stores the date part, omitting the time. DATE
has a smaller storage requirement than DATETIME
.FLOAT
data type in SQL Server?
FLOAT
is used to store approximate numeric values with floating decimal points. It is typically used for scientific calculations requiring precision over a wide range of values.BIT
data type in SQL Server?
BIT
data type stores Boolean values (0
, 1
, or NULL
). It is used to represent logical binary data, such as true/false or yes/no values.TEXT
and NTEXT
data types used for in SQL Server?
TEXT
and NTEXT
are used to store large amounts of text data. TEXT
stores non-Unicode characters, while NTEXT
is used for Unicode characters. They are being deprecated in newer versions.DECIMAL
and NUMERIC
data types in SQL Server?
DECIMAL
and NUMERIC
are functionally identical. Both store exact numeric values with a defined precision and scale, commonly used for storing monetary values or fixed-point numbers.MONEY
data type used for in SQL Server?
MONEY
data type is used to store currency values. It has a fixed precision and scale (4 decimal places) and is ideal for handling financial data.UNIQUEIDENTIFIER
data type in SQL Server.
UNIQUEIDENTIFIER
data type stores globally unique identifiers (GUIDs), which are used to generate unique values across systems, typically for use as primary keys.XML
data type in SQL Server?
XML
data type is used to store XML documents or fragments in a column. It allows storing and querying XML data directly within SQL Server.NULL
values. Only one primary key is allowed per table.NULL
values, while a unique key can. A table can have multiple unique keys but only one primary key.NULL
values.NOT NULL
constraint ensures that a column cannot store NULL
values. This constraint is used to enforce data integrity by requiring values to be entered in a column.NOT NULL
constraint to an existing column using the ALTER TABLE
statement: ALTER TABLE table_name ALTER COLUMN column_name datatype NOT NULL;
.DEFAULT
constraint automatically provides a value for a column when no value is specified during an INSERT
operation. It helps ensure that all rows have valid data even if not explicitly provided.DEFAULT
constraint cannot be used to set NULL
values. It is only used to assign a non-null default value when no value is provided during an insert.DEFAULT
keyword in the CREATE TABLE
statement, for example: CREATE TABLE table_name (column_name datatype DEFAULT value);
.CHECK
constraint ensures that values in a column meet a specific condition, such as being within a specified range or matching a particular pattern.CHECK
constraint to an existing column using the ALTER TABLE
statement: ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (condition);
.CHECK
and WHERE
clauses in SQL Server?
CHECK
constraint is used to enforce rules on column values in a table, whereas the WHERE
clause is used in queries to filter rows based on conditions.INSERT
or UPDATE
) from being completed, ensuring data integrity.ALTER TABLE
statement with the NOCHECK
option, though this is not recommended for production environments as it could lead to data integrity issues.