Basic SQL Int Ques
Basic SQL Int Ques
1. What is the difference between SQL, Oracle, MySQL, and SQL Server?
SQL is the name of the language used to query databases and follows a standard. Oracle,
MySQL and SQL Server are different implementations or versions of a database
management system, which implement the SQL standard and build on it in different
ways. Oracle database is targeted at large companies, SQL Server is owned by Microsoft,
and MySQL is owned by Oracle but targeted toward smaller companies and systems.
4. What are the different DDL commands in SQL? Give a description of their purpose.
● CREATE: creates objects in the database
● ALTER: makes changes to objects in the database
● DROP: removes objects from the database
● TRUNCATE: deletes all data from a table
● COMMENT: adds comments to the data dictionary
● RENAME: renames an object in the database
5. What are the different DML commands in SQL? Give a description of their purpose.
● SELECT: retrieve or view data from the database
● INSERT: add new records into a table
● UPDATE: change existing records in a table
● DELETE: removes data from a table
● MERGE: performs an UPSERT operation, also known as insert or update.
● CALL: runs a PL/SQL procedure or Java program
● EXPLAIN PLAN: explains the way the data is loaded
● LOCK TABLE: helps control concurrency
This query retrieves all employees whose total sales exceed 10,000 from the
employee_sales view, just as if it were a table.
16. If a table contains duplicate rows, will a query display duplicate values by default?
How can you eliminate duplicate rows from a query result?
Yes, they will be displayed by default. To eliminate duplicate records, you use the
DISTINCT keyword after the word SELECT.
In SQL, constraints are rules applied to columns or tables that enforce data integrity and
ensure that the data adheres to certain conditions. Constraints help maintain accuracy,
consistency, and reliability of the data in a database.
1. NOT NULL:
o Ensures that a column cannot have a NULL value (i.e., it must always contain a
value).
2. UNIQUE:
o Ensures that all values in a column (or a group of columns) are distinct,
meaning no duplicate values are allowed.
3. PRIMARY KEY:
o Combines the NOT NULL and UNIQUE constraints. It uniquely identifies each
row in a table. Each table can have only one primary key, and it cannot
contain NULL values.
4. FOREIGN KEY:
o Ensures referential integrity between two tables by creating a relationship
between a column in one table (the foreign key) and the PRIMARY KEY of
another table.
5. CHECK:
o Ensures that values in a column satisfy a specific condition.
6. DEFAULT:
o Assigns a default value to a column if no value is provided when inserting
data.
1. What are the different JOIN types and what do they do?
The different join types in Oracle SQL are:
For example, if table A has 10 records and table B has 8 records, then the cross join will
result in 80 (or 10 x 8) records. The result can also be called a “cartesian product”.
The WHERE clause is run to remove data before grouping. The HAVING clause is run on
data after it has been grouped. This also means the WHERE clause cannot operate on
aggregate functions calculated as part of the group.
The WHERE clause cannot include any checks on the aggregate column – even if a
GROUP BY has been performed.
This is because the WHERE happens before the grouping, so there is no way for the
WHERE clause to know what the value of the COUNT function is.
To resolve this, use the HAVING clause to check for COUNT(*) > 5.
1. What is the default sort order using ORDER BY? How can it be changed?
The default sort order is ascending. This can be changed by specifying the word DESC
after any column name in the ORDER BY clause. The word ASC can be used instead to
specify ascending order.
1. What does UNION do? What’s the difference between UNION and UNION
ALL?
Union allows you to combine two sets of results into one result. It’s different to UNION
ALL because UNION removes duplicate values and UNION ALL does not.
3. Write a query to display the 5th highest employee salary in the employee
table
SELECT * FROM ( SELECT employee_id, first_name, last_name, salary,
DENSE_RANK() OVER (ORDER BY salary DESC NULLS LAST)
rank_val FROM employee )
WHERE rank_val = 5;
This could also be done using the ROW_NUMBER function. It’s one of those interview
questions in
SQL that can have multiple answers, but as long as you provide an answer to it, you should
be OK.
1. What is cardinality?
Cardinality refers to the uniqueness of values in a column. High cardinality means that
there is a large percentage of unique values. Low cardinality means there is a low
percentage of unique values.
3. What is normalization?
Normalization is the process of organizing your data into tables that adhere to certain
rules. It aims to make the process of selecting, inserting, updating, and deleting data more
efficient and reduce data issues that may appear otherwise.
There are three popular normal forms, named first/second/third normal form. Third
normal form is commonly used as a goal, but there are normal forms after third normal
form that are occasionally used.
4. What is denormalization?
Denormalization is the process of converting a normalized database into a series of tables
that are not normalized. These deformalized tables often contain records that refer to the
same value, so updating them is not as efficient. However, the aim of this process is
usually to prepare the data for a data warehouse, so the goal is the efficient reading of
data. It often results in a smaller number of tables, each of which has more columns than
normalized tables.
5. What do OLTP and OLAP mean and how are they different?
OLTP stands for OnLine Transaction Processing and refers to databases that are
designed for regular transactions of inserting, updating, and deleting data. This often
includes a normalised database and is linked to an application used during business hours
for people to do their job.
OLAP stands for OnLine Analytical Processing and refers to databases that are designed
for analysis and reporting. They are focused on SELECT queries and often contain
denormalised database designs. They are often used by reporting systems to analyse
data from other OLTP systems.
Functions
1. What are the case manipulation functions in Oracle SQL?
To change the case of a string in Oracle SQL you can use UPPER, LOWER, or INITCAP,
LENGTH,TRIM, REPLACE.
3. How can you search for a value in a column when you don’t have the exact match to
search for?
If you don’t know the exact match, you can use wildcards along with LIKE. The wildcards
are the % symbol for any number of characters, and the _ symbol for a single character.
4. What is an execution plan? How can you view the execution plan?
An execution plan is a graphic or text visualization of how the database’s optimizer will
run a query. They are useful for helping a developer understand and analyse the
performance of their query. To find the execution plan of a query, add the words
“EXPLAIN PLAN FOR” before your query. The query won’t run, but the execution plan for
the query will be displayed.
5. Is NULL the same as a zero or blank space?If not, what is the difference?
No, they are different. NULL represents an unknown value. Zero represents the number
zero, and a blank space represents a character string with no data. NULL is compared
differently to a zero and a blank space and must use comparisons like IS NULL or IS
NOT NULL.
9. What’s the difference between % and _for pattern matching (e.g. in the
LIKE operator)?
The difference is the % sign will match one or more characters, but the _ sign will match
only one character.
11. What is a temp table, and when would you use one?
A temp table (or temporary table) is a database table that exists temporarily on the
system. It allows
you to store the results of a query for use later in a session. They are useful if you have a
large number
of results and you want to use them again.
A temporary table, by default, is only accessible by you. Global temporary tables can be
accessed by
others.
Temporary tables are automatically deleted when the connection that created them is
closed.
Follow me on LinkedIn