SQL Interview Questions Cheat Sheet
SQL Interview Questions Cheat Sheet
SQL Interview Questions Cheat Sheet
The following section covers basic concepts, with the following section focusing on
intermediate and advanced SQL questions.
qty_on_hand NUMBER(5);
BEGIN
END;
BEGIN
END;
Inner Join
Rows are returned when there is at least one match of rows between the tables.
from customers c
on c.customer_id = o.customer_id
/* customers and orders are two tables. Data will be displayed from the two
tables where the customer_id from customer table matches
Right Join
Right join returns all rows from the right table and those which are shared between the
tables. If there are no matching rows in the left table, it will still return all the rows from
the right table.
from customers c
on c.customer_id = o.customer_id
/* customers and orders are two tables. All rows from the Orders table is
returned with matching rows from the Customers table if any */
Left Join
Left join returns all rows from the Left table and those which are shared between the
tables. If there are no matching rows in the right table, it will still return all the rows from
the left table.
from customers c
on c.customer_id = o.customer_id
/* customers and orders are two tables. All rows from the customers table
is returned with matching rows from the orders table if any */
Full Join
Full join return rows when there are matching rows in any one of the tables. This means
it returns all the rows from the left-hand side table and all the rows from the right-hand
side table.
select first_name, last_name, order_date, order_amount
from customers c
on c.customer_id = o.customer_id
/* customers and orders are two tables. All rows from the Orders table and
customer table are returned */
For example, suppose you store the string ‘Database’ in a CHAR(20) field and a
VARCHAR2(20) field.
The CHAR field will use 22 bytes (2 bytes for leading length).
The VARCHAR2 field will use 10 bytes only (8 for the string, 2 bytes for
leading length).
● NOT NULL: Restricts NULL value from being inserted into a column
The primary key is a field in the table which uniquely identifies a row. It cannot be NULL
Foreign Key
A foreign key is a field in one table that is a primary key in another table. A relationship
is created between the two tables by referencing the foreign key of one table with the
In the example below, the employee_id_ref in the salary table is the foreign key.
Unique Key
The Unique Key uniquely identifies a record in a table. There can be many unique key
In the table above Emp_id is the primary key but Government_id is the unique key. You
may want the Government_id to be unique for every employee. Since the data belongs
to the government, you may not want it to be the primary key.
8. What is a trigger?
Triggers are stored programs that get automatically executed when an event such as
INSERT, DELETE, and the UPDATE (DML) statement occurs. Triggers can also be
evoked in response to Data definition statements(DDL) and database operations, for
example, SERVER ERROR, LOGON.
on database
for
create_table,alter_table,drop_table
as
print'you can not create ,drop and alter table in this database'
rollback;
on emp
for
insert,update,delete
as
rollback;
● Atomicity: The transaction must happen fully and cannot be left midway
● Consistency: This maintains integrity constraints to ensure valid data enters the
database
Quarter qq, q
Month Mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
Minute mi, n
Second ss, s
Millisecond ms
Microsecond mcs
Nanosecond ns
SQL> SAVEPOINT A
1 row inserted.
SQL> SAVEPOINT B
1 row inserted.
SQL> ROLLBACK TO B;
Rollback complete.
ID MSG
-------- -----------
1 Savepoint A
A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT OUTER join.
The default is INNER join.
If the tables COUNTRIES and CITIES have two common columns named COUNTRY
and COUNTRY_ISO_CODE, the following two SELECT statements are equivalent:
,@Name VARCHAR(100)
,@Country VARCHAR(100)
SET @Counter = 1
FOR
SELECT CustomerId, Name, Country
FROM Customers
--OPEN CURSOR.
OPEN PrintCustomers
WHILE @@FETCH_STATUS = 0
BEGIN
IF @Counter = 1
BEGIN
PRINT '------------------------------------'
END
--INCREMENT COUNTER.
END
CLOSE PrintCustomers
DEALLOCATE PrintCustomers
11. State one situation where set-based solutions are better than
cursor-based solutions.
Set-based solutions provide better performance when you are working on a result set,
as opposed to one row at a time. They are concise and more readable.
1 John 9176612345,9176645698
2 Susie 9176645789
3 Jim 9176696325
In the above table the field ‘phonenum’ is a multi-valued attribute, so it is not in 1NF.
------------------
1 John 9176612345
1 John 9176645698
2 Susie 9176645789
3 Jim 9176696325
The table must be in First Normal Form, and it should have a single column as its
primary key. 2NF tries to reduce the redundant data getting stored in memory. To
transform the table above into 2NF, we split the table into two tables:
1. John
2 Susie
3. Jim
StudiD Phonenum /* studentphonenumber table */
------------------
1 9176612345
1 9176645698
2 9176645789
3 9176696325
The table must be in Second Normal Form and must have no transitive functional
Consider the EMPLOYEE_DETAIL table: This table is not in the third normal form
because the fields emp_state and emp_city depend on emp_zip and not on the primary
key emp_id.
EMPLOYEE table:
EMPLOYEE_ZIP table:
201010 CT Monro
02228 TX Dallas
060007 IL Chicago
A table can have only one clustered index. In this type of index, it reorders the table
based on the key values and physically stores them in that order.
The non-clustered index does not have the physical ordering of the data in the table; it
has a logical order.
ON student(name ASC)
The script above creates a non-clustered index on the “name” column of the student
table — the index sorts by name in ascending order. The table data and index will be
stored in different places.
category VARCHAR(25)
);
quantity INT,
min_level INT,
max_level INT,
CONSTRAINT fk_inv_product_id
ON DELETE CASCADE
);
The Products table is the parent table and the inventory table is the child table. If a
productid is deleted from the parent table all the inventory records for that productid will
be deleted from the child table
BEGIN TRANSACTION;
COMMIT TRANSACTION;
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
(COL1 integer,
COL2 varchar(30),
COL3 varchar(50),
The following SQL creates a view that shows all customers from Brazil:
FROM Customers
The following SQL statement selects only the DISTINCT values from the "Country"
column in the "Customers" table:
SELECT
FROM
employees
WHERE
department_id IN (SELECT
department_id
FROM
departments
WHERE
location_id = 1700)
The query placed within the parentheses is called a subquery. It is also known as an
inner query or inner select. The query that contains the subquery is called an outer
query or an outer select.
The following are examples of system privileges that can be granted to users:
a user session
The following examples are object privileges that can be granted to users:
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
DEPTNO NUMBER(2));
2 FROM emp;
VARIANCE(SAL)
-------------
759056.818
UNION
ORDER BY City;
8. Where are stored procedures stored and can we call one inside
another?
Stored Procedures are stored in the Data Dictionary of the database.
Yes, we can call a stored procedure from another stored procedure. For example,
Procedure2 is the procedure which is called Procedure1. Both Procedure1 and
Procedure2 can have business logic implemented in it.
9. Does the data stored in the stored procedure increase access time
or execution time?
Data stored in stored procedures can be retrieved much faster than the data stored in
the SQL database. Data can be precompiled and stored in stored procedures. This
reduces the time gap between query and compiling as the data has been pre-compiled
and stored in the procedure. Procedures may or may not return values.
Table: Employee_Details
1. Row integrity
2. Column integrity
3. Referential integrity
4. User-defined integrity
each other. An example is the fact that an employee name, id, salary might
DELETE TRUNCATE
Deletes one row from the table Deletes all rows from the table
16. What is the difference between null, zero and blank space?
NULL refers to a value that is unknown, not available, inapplicable or unassigned. Zero
is a number, and blank space is treated as a character.
7. TRIM: Removes the defined character from beginning and end or both
of characters
Example:
● By using EXEC
● By using sp_executesql
Example:
S and E are alias names for student table and exam table respectively.
1. Case
2. Kana
3. Width
4. Accent
REPLACE: replaces the existing characters of all the occurrences. The syntax is: