SQL Interview Questions
This page consists of SQL interview questions and answers.
We use UNION to combine the content of two structurally compatible tables into one table.
UNION
The UNION omits any duplicate whereas UNION ALL keeps the duplicate records in the result.
UNION ALL
There are 4 types of JOIN in SQL.
JOIN or INNER JOIN is the most common join and it returns all the rows from the left table and right table where the join condition is satisfied.
Click here for more detail on INNER JOIN.
LEFT JOIN or LEFT OUTER JOIN is used to return all the rows from the left table and only the matching rows from the right table where the join condition is satisfied.
RIGHT JOIN or RIGHT OUTER JOIN is used to return all the rows from the right table and only the matching rows from the left table where the join condition is satisfied.
FULL JOIN or FULL OUTER JOIN returns all the rows from the left and the right table if there is a match in any one of the table.
A primary key is a column or collection of columns in a table that can uniquely identify a row.
A foreign key is a column or collection of columns in a table that points at the primary key of some other table.
We use foreign key to link two tables.
firstname
X
For this we will use LIKE.
LIKE
Lets say we have a students table and it has a column firstname which holds the first name of the students.
students
To get all the students whose first name starts with X we will write the following query.
mysql> SELECT * FROM students WHERE firstname LIKE 'X%';
mysql> denotes MySQL command line.
mysql>
To list all the students in ascending order we can write the following query.
mysql> SELECT * FROM students ORDER BY firstname ASC;
By default the result is returned in ascending order so, we can drop the ASC from the query and just use the following query.
ASC
mysql> SELECT * FROM students ORDER BY firstname;