SQL Interview Questions
This page consists of SQL interview questions and answers.
The IN operator is used to check for values in a given set.
IN
For example, if we want to list the details of students having studentid lets say s01, s03 and s04 then we will use the IN operator.
studentid
s01
s03
s04
mysql> SELECT * FROM student WHERE studentid IN ('s01', 's03', 's04');
The BETWEEN operator is used to retrieve rows based on a given range of values.
BETWEEN
For example, if we want to list all the students born from 1st January 2000 to 31st December 2000 then we will use the BETWEEN operator.
mysql> SELECT * FROM student WHERE birthday BETWEEN '2000-01-01' AND '2000-12-31';
mysql> denotes MySQL command line.
mysql>
firstname
For this we will use the LIKE operator.
LIKE
Following query will fetch all the students whose firstname starts with 'G'.
mysql> SELECT * FROM student WHERE firstname LIKE 'G%';
lastname
Following query will list all the students whose lastname ends with 'a'.
mysql> SELECT * FROM student WHERE lastname LIKE '%a';
Following is the query that will return all the students whose firstname or lastname contains the string 'ai'.
mysql> SELECT * FROM student WHERE firstname LIKE '%ai%' OR lastname LIKE '%ai%';
Following are the differences between a primary key and a unique constraint.
Primary key can't have NULL value whereas a unique constraint can have the NULL value.
NULL
There can only be ONE primary key in a given table whereas there can be one and more number of unique constraints.
A VIEW is like a virtual table that consists of columns from one or more number of tables but no data.
Lets say we have a students table and it consists of the following columns.
students
Now lets say we want to create a view that will contain only the following columns.
Then we can write the following SQL.
mysql> CREATE VIEW students_name AS SELECT studentid, firstname, lastname FROM students;
So, if we want to list all the students name using the view students_name then we can write the following query.
students_name
mysql> SELECT * FROM students_name;
To delete a view use DROP VIEW.
DROP VIEW
For example, if we have a view lets say students_score_view then we can delete it by writing the following SQL.
students_score_view
mysql> DROP VIEW students_score_view;
SQL or Structured Query Language is a language used to interact with database. MySQL is a RDBMS (Relational Database Management System) that uses SQL to work with the data.
An entity is a real world object whose data can be stored in a database table.
For example, a student is an entity and the data of a student can be stored in a students table.
A relationship is a link that connects two or more tables.
For example we can say the customers table has a relationship with the orders table.
customers
orders