Tricky SQL Interview Questions
1. Can the primary key be NULL?
No, a primary key cannot be NULL, and it must have a unique, non-null value.
2. Can a primary key have duplicates?
No, a primary key cannot have duplicates, and it enforces uniqueness for each
record.
3. Can a unique key always be used as a primary key?
We cannot always use a unique key as a primary key because a primary key
needs to satisfy more requirements. A primary key must always have a value
(cannot be NULL) and must not change. Also, a table can only have a single
primary key, whereas it can have multiple unique keys.
4. What are the functions of SQL?
SQL functions are a set of statements that perform a specific task. Generally,
the SQL functions take inputs, perform certain operations, and return results
as a single value or a table. They run in the database to perform activities on
datasets or database objects.
5. How many SQL functions are there?
SQL provides numerous functions,
Aggregate functions (COUNT, SUM) and scalar functions (UPPER, CONCAT),
with variations for different database systems.
6. What is any function in SQL?
"ANY" in SQL is used in conjunction with comparison operators to evaluate
whether any of the values in a subquery meet a specified condition.
7. Can I use SQL MAX Function for Non-numeric Columns?
Yes, you can use the SQL MAX function for non-numeric columns. When you
use the MAX function, the lexicographic order is considered to generate the
output. It compares similar characters in the two strings from left to right to
identify which string comes first. For example, ‘a’ will have high priority than
‘b’.
8. What are NULL values in a Table?
According to SQL, any datatype may have a NULL value. A particular label
called a NULL value is used in SQL to denote a data value that doesn't exist in
the database. A field with spaces or a zero value differs from one with a NULL
value.
9. What do you mean by the Primary Key of a table?
You can uniquely identify each record in a table using the Primary Key. A
Primary Key should be UNIQUE and not NULL. For example, in a
student_details table, the registration number can be considered a Primary
Key. There can only be one Primary Key in a table.
10. What is CAST () in SQL?
In SQL, the cast function is used for converting a value of one data type to
another. We can convert the data type by specifying the data type we want it
to convert to. This function accepts two parameters, i.e., the value we want to
convert and the desired data type.
11.What is CAST () and convert () function in SQL Server?
Cast and convert functions transform data types. Both convert a value from
one type to another, serving similar purposes in database queries or
programming.
12. What is CAST in MySQL query?
MySQL's CAST() function converts values between data types within
expressions, commonly used in WHERE, HAVING, and JOIN clauses, similar to
the CONVERT() function.
13.Why we will use CAST in SQL?
The CAST function in SQL converts values between data types, ensuring
compatibility during operations like calculations or comparisons and providing
accurate results.