SQL Q&A
SQL Q&A
SQL vs MySQL
SQL MySQL
A Database Management
System (DBMS) is a software application that interacts with the user,
applications, and the database itself to capture and analyze data. A
database is a structured collection of data.
A DBMS allows a user to interact with the database. The data stored in the
database can be modified, retrieved and deleted and can be of any type
like strings, numbers, images, etc.
A SELECT command gets zero or more rows from one or more database
tables or views. The most frequent data manipulation language (DML)
command is SELECT in most applications. SELECT queries define a result
set, but not how to calculate it, because SQL is a declarative programming
language.
The following are some frequent SQL clauses used in conjunction with a
SELECT query:
WHERE clause: In SQL, the WHERE clause is used to filter records that are
required depending on certain criteria.
ORDER BY clause: The ORDER BY clause in SQL is used to sort data in
ascending (ASC) or descending (DESC) order depending on specified
field(s) (DESC).
GROUP BY clause: GROUP BY clause in SQL is used to group entries with
identical data and may be used with aggregation methods to obtain
summarised database results.
HAVING clause in SQL is used to filter records in combination with the
GROUP BY clause. It is different from WHERE, since the WHERE clause
cannot filter aggregated records.
The UNION operator is used to combine the results of two tables while
also removing duplicate entries.
The MINUS operator is used to return rows from the first query but not
from the second query.
The INTERSECT operator is used to combine the results of both queries
into a single row.
Before running either of the above SQL statements, certain requirements
must be satisfied –
Within the clause, each SELECT query must have the same amount of
columns.
The data types in the columns must also be comparable.
In each SELECT statement, the columns must be in the same order.
To start the result set, move the cursor over it. Before obtaining rows from
the result set, the OPEN statement must be executed.
To retrieve and go to the next row in the result set, use the FETCH
command.
OLTP stands for online transaction processing, whereas OLAP stands for
online analytical processing. OLTP is an online database modification
system, whereas OLAP is an online database query response system.
You may use the NVL function to replace null values with a default value.
The function returns the value of the second parameter if the first
parameter is null. If the first parameter is anything other than null, it is left
alone.
This function is used in Oracle, not in SQL and MySQL. Instead of NVL()
function, MySQL have IFNULL() and SQL Server have ISNULL() function.
A) CONCAT (joining two or more values): This function is used to join two
or more values together. The second string is always appended to the end
of the first string.
G) TRIM: This function removes all defined characters from the beginning,
end, or both ends of a string. It also reduced the amount of wasted space.
Q19. Write the SQL query to get the third maximum salary
of an employee from a table named employees.
Employee table
employee_nam salar
e y
2400
A
0
3400
C
0
5500
D
0
7500
E
0
2100
F
0
4000
G
0
5000
H
0
SELECT * FROM(
WHERE r=&n;
The RANK() function in the result set defines the rank of each row within
your ordered partition. If both rows have the same rank, the next number
in the ranking will be the previous rank plus a number of duplicates. If we
have three records at rank 4, for example, the next level indicated is 7.
A SELECT command gets zero or more rows from one or more database
tables or views. The most frequent data manipulation language (DML)
command is SELECT in most applications. SELECT queries define a result
set, but not how to calculate it, because SQL is a declarative programming
language.
The following are some frequent SQL clauses used in conjunction with a
SELECT query:
SQL Essentials Training & Certification
Course Duration
Real-life Case Studies
Assignments
Lifetime Access
Explore Curriculum
WHERE clause: In SQL, the WHERE clause is used to filter records that are
required depending on certain criteria.
ORDER BY clause: The ORDER BY clause in SQL is used to sort data in
ascending (ASC) or descending (DESC) order depending on specified
field(s) (DESC).
GROUP BY clause: GROUP BY clause in SQL is used to group entries with
identical data and may be used with aggregation methods to obtain
summarised database results.
HAVING clause in SQL is used to filter records in combination with the
GROUP BY clause. It is different from WHERE, since the WHERE clause
cannot filter aggregated records.
The MINUS operator is used to return rows from the first query but not
from the second query.
Within the clause, each SELECT query must have the same amount of
columns.
To start the result set, move the cursor over it. Before obtaining rows from
the result set, the OPEN statement must be executed.
To retrieve and go to the next row in the result set, use the FETCH
command.
SQL is a database query language that allows you to edit, remove, and
request data from databases. The following statements are a few
examples of SQL statements:
SELECT
INSERT
UPDATE
DELETE
CREATE DATABASE
ALTER DATABASE
SQL skills aid data analysts in the creation, maintenance, and retrieval of
data from relational databases, which divide data into columns and rows.
It also enables users to efficiently retrieve, update, manipulate, insert, and
alter data.
The most fundamental abilities that a SQL expert should possess are:
1. Database Management
2. Structuring a Database
3. Creating SQL clauses and statements
4. SQL System SKills like MYSQL, PostgreSQL
5. PHP expertise is useful.
6. Analyze SQL data
7. Using WAMP with SQL to create a database
8. OLAP Skills
Step 2: Moreover, click on the SQL Server Management Studio link and
tap on Save File.
Step 3: Save this file to your local drive and go to the folder.
Step 4: The setup window will appear, and here you can choose the
location where you want to save the file.
Step 5: Click on Install.
Step 6: Close the window after the installation is complete.
Step 7: Furthermore, go back to your Start Menu and search for SQL
server management studio.
Step 8: Furthermore, double-click on it, and the login page will appear
once it shows up.
Step 9: You should be able to see your server name. However, if that’s
not visible, click on the drop-down arrow on the server and tap on Browse.
After that, the SQL server will connect, and Windows 11 will run good.
At least one set of WHEN and THEN commands makes up the SQL Server
CASE Statement. The condition to be tested is specified by the WHEN
statement. If the WHEN condition returns TRUE, the THEN sentence
explains what to do.
When none of the WHEN conditions return true, the ELSE statement is
executed. The END keyword brings the CASE statement to a close.
1CASE
2WHEN condition1 THEN result1
3WHEN condition2 THEN result2
4WHEN conditionN THEN resultN
5ELSE result
6END;
Q35. NoSQL vs SQL
In summary, the following are the five major distinctions between SQL and
NoSQL:
Relational databases are SQL, while non-relational databases are NoSQL.
BLOB stands for Binary Huge Objects and can be used to store binary
data, whereas TEXT may be used to store a large number of strings. BLOB
may be used to store binary data, which includes images, movies, audio,
and applications.
BLOB values function similarly to byte strings, and they lack a character
set. As a result, bytes’ numeric values are completely dependent on
comparison and sorting.
TEXT values behave similarly to a character string or a non-binary
string. The comparison/sorting of TEXT is completely dependent on the
character set collection.
If the SQL table has duplicate rows, the duplicate rows must be removed.
A stored procedure is a piece of prepared SQL code that you can save and
reuse again and over.
So, if you have a SQL query that you create frequently, save it as a stored
procedure and then call it to run it.
You may also supply parameters to a stored procedure so that it can act
based on the value(s) of the parameter(s) given.
AS
sql_statement
GO;
EXEC procedure_name;
Black Box Testing is a software testing approach that involves testing the
functions of software applications without knowing the internal code
structure, implementation details, or internal routes. Black Box Testing is
a type of software testing that focuses on the input and output of software
applications and is totally driven by software requirements and
specifications. Behavioral testing is another name for it.
Prior to the introduction of MySQL 5.5 in December 2009, MyISAM was the
default storage engine for MySQL relational database management
system versions. It’s based on the older ISAM code, but it comes with a
lot of extra features. Each MyISAM table is split into three files on disc (if it
is not partitioned). The file names start with the table name and end with
an extension that indicates the file type. The table definition is stored in
a.frm file, however this file is not part of the MyISAM engine; instead, it is
part of the server. The data file’s suffix is.MYD (MYData). The index file’s
extension is.MYI (MYIndex). If you lose your index file, you may always
restore it by recreating indexes.
employee_name salary
A 24000
C 34000
D 55000
E 75000
F 21000
G 40000
H 50000
SELECT * FROM(
WHERE r=&n;
Table: StudentInformation
Field: Stu Id, Stu Name, Stu Marks
A JOIN clause is used to combine rows from two or more tables, based on
a related column between them. It is used to merge two tables or retrieve
data from there. There are 4 types of joins, as you can refer to below:
Inner join: Inner Join in SQL is the most common type of join. It is
used to return all the rows from multiple tables where the join
condition is satisfied.
Left Join: Left Join in SQL is used to return all the rows from the left
table but only the matching rows from the right table where the join
condition is fulfilled.
Right Join: Right Join in SQL is used to return all the rows from the
right table but only the matching rows from the left table where the
join condition is fulfilled.
Full Join: Full join returns all the records when there is a match in
any of the tables. Therefore, it returns all the rows from the left-
hand side table and all the rows from the right-hand side table.
Both Char and Varchar2 are used for characters datatype but varchar2 is
used for character strings of variable length whereas Char is used for
strings of fixed length. For example, char(10) can only store 10 characters
and will not be able to store a string of any other length whereas
varchar2(10) can store any length i.e 6,8,2 in this variable.
Constraints in SQL are used to specify the limit on the data type of the
table. It can be specified while creating or altering the table statement.
The sample of constraints are:
NOT NULL
CHECK
DEFAULT
UNIQUE
PRIMARY KEY
FOREIGN KEY
DELETE vs TRUNCATE
DELETE TRUNCATE
Delete command is used to delete a row Truncate is used to delete all the
in a table. rows from a table.
You can rollback data after using delete
You cannot rollback data.
statement.
Apart from this SQL Interview Questions blog, if you want to get trained
from professionals on this technology, you can opt for structured training
from edureka!
Data Integrity defines the accuracy as well as the consistency of the data
stored in a database. It also defines integrity constraints to enforce
business rules on the data when it is entered into an application or a
database.
The differences between the clustered and non clustered index in SQL
are :
1. Clustered index is used for easy retrieval of data from the database and its
faster whereas reading from non clustered index is relatively slower.
2. Clustered index alters the way records are stored in a database as it sorts
out rows by the column which is set to be clustered index whereas in a
non clustered index, it does not alter the way it was stored but it creates a
separate object within a table which points back to the original table rows
after searching.
3. One table can only have one clustered index whereas it can have
many non clustered index.
Q54. Write a SQL query to display the current date?
The phase that identifies a plan for evaluation query which has the least
estimated cost is known as query optimization.
Entities: A person, place, or thing in the real world about which data can
be stored in a database. Tables store data that represents one type of
entity. For example – A bank database has a customer table to store
customer information. The customer table stores this information as a set
of attributes (columns within the table) for each customer.
Unique Index:
This index does not allow the field to have duplicate values if the column
is unique indexed. If a primary key is defined, a unique index can be
applied automatically.
Clustered Index:
This index reorders the physical order of the table and searches based on
the basis of key values. Each table can only have one clustered index.
Non-Clustered Index:
Non-Clustered Index does not alter the physical order of the table and
maintains a logical order of the data. Each table can have many
nonclustered indexes.
Apart from this SQL Interview Questions Blog, if you want to get trained
from professionals on this technology, you can opt for structured training
from edureka!
OLTP stands for online transaction processing, whereas OLAP stands for
online analytical processing. OLTP is an online database modification
system, whereas OLAP is an online database query response system.
Using the INTO operator to fetch the records of one table into a new table
while setting a WHERE clause to false for all entries, it is possible to create
empty tables with the same structure. As a result, SQL creates a new
table with a duplicate structure to accept the fetched entries, but nothing
is stored into the new table since the WHERE clause is active.
Q65. What is PostgreSQL?
The RANK() function in the result set defines the rank of each row within
your ordered partition. If both rows have the same rank, the next number
in the ranking will be the previous rank plus a number of duplicates. If we
have three records at rank 4, for example, the next level indicated is 7.
SQL injection is a sort of flaw in website and web app code that allows attackers to take
control of back-end processes and access, retrieve, and delete sensitive data stored in
databases. In this approach, malicious SQL statements are entered into a database entry field,
and the database becomes exposed to an attacker once they are executed. By utilising data-
driven apps, this strategy is widely utilised to get access to sensitive data and execute
administrative tasks on databases. SQLi attack is another name for it.
Getting access to secret data in order to change a SQL query to acquire the desired results.
UNION attacks are designed to steal data from several database tables.
Examine the database to get information about the database’s version and structure
SQL aggregate functions provide information about a database’s data. AVG, for example,
returns the average of a database column’s values.
SQL provides seven (7) aggregate functions, which are given below:
Q70. What is the default ordering of data using the ORDER BY clause? How could it be
changed?
The ORDER BY clause in MySQL can be used without the ASC or DESC modifiers. The
sort order is preset to ASC or ascending order when this attribute is absent from the ORDER
BY clause.
The SQL DISTINCT keyword is combined with the SELECT query to remove all duplicate
records and return only unique records. There may be times when a table has several
duplicate records.
The DISTINCT clause in SQL is used to eliminate duplicates from a SELECT statement’s
result set.
Q72. What are the syntax and use of the COALESCE function?
From a succession of expressions, the COALESCE function returns the first non-NULL
value. The expressions are evaluated in the order that they are supplied, and the function’s
result is the first non-null value. Only if all of the inputs are null does the COALESCE
method return NULL.
ACID stands for Atomicity, Consistency, Isolation, Durability. It is used to ensure that the
data transactions are processed reliably in a database system.
Atomicity: Atomicity refers to the transactions that are completely done or failed where
transaction refers to a single logical operation of a data. It means if one part of any
transaction fails, the entire transaction fails and the database state is left unchanged.
Consistency: Consistency ensures that the data must meet all the validation rules. In simple
words, you can say that your transaction never leaves the database without completing its
state.
Isolation: The main goal of isolation is concurrency control.
Durability: Durability means that if a transaction has been committed, it will occur whatever
may come in between such as power loss, crash or any sort of error.
Want to upskill yourself to get ahead in your career? Check out this video in this SQL Interview
Questions
Trigger in SQL is are a special type of stored procedures that are defined
to execute automatically in place or after data modifications. It allows you
to execute a batch of code when an insert, update or any other query is
executed against a specific table.
1. Arithmetic Operators
2. Logical Operators
3. Comparison Operators
Apart from this SQL Interview Questions blog, if you want to get trained
from professionals on this technology, you can opt for structured training
from edureka!
A NULL value is not at all same as that of zero or a blank space. NULL
value represents a value which is unavailable, unknown, assigned or not
applicable whereas a zero is a number and blank space is a character.
The cross join produces the cross product or Cartesian product of two
tables whereas the natural join is based on all the columns having the
same name and data types in both the tables.
Correlated subquery: These are queries which select the data from a
table referenced in the outer query. It is not considered as an independent
query as it refers to another table and refers the column in a table.
Apart from this SQL Interview Questions Blog, if you want to get trained
from professionals on this technology, you can opt for structured training
from edureka!
To display name of the employees that begin with ‘A’, type in the below
command:
Group functions work on the set of rows and return one result per group.
Some of the commonly used group functions are: AVG, COUNT, MAX, MIN,
SUM, VARIANCE.
Relation or links are between entities that have something to do with each
other. Relationships are defined as the connection between the tables in a
database. There are various relationships, namely:
Example of BETWEEN:
SQL clause helps to limit the result set by providing a condition to the
query. A clause helps to filter the rows from the entire set of records.
For example – WHERE, HAVING clause.
Apart from this SQL Interview Questions Blog, if you want to get trained
from professionals on this technology, you can opt for a structured
training from edureka! Click below to know more.
HAVING clause can be used only with SELECT statement. It is usually used
in a GROUP BY clause and whenever GROUP BY is not used, HAVING
behaves like a WHERE clause.
Having Clause is only used with the GROUP BY function in a query
whereas WHERE Clause is applied to each row before they are a part of
the GROUP BY function in a query.
Q94. How can you fetch common records from two tables?
You can fetch common records from two tables using INTERSECT. For
example:
LOWER(‘string’)
UPPER(‘string’)
INITCAP: This function returns the string with the first letter in
uppercase and rest of the letters in lowercase. Syntax:
INITCAP(‘string’)
Apart from this SQL Interview Questions blog, if you want to get trained
from professionals on this technology, you can opt for a structured
training from edureka! Click below to know more.
ALIAS command in SQL is the name that can be given to any table or a
column. This alias name can be referred in WHERE clause to identify a
particular table or a column.
For example-
You can fetch alternate records i.e both odd and even row numbers. For
example- To display even numbers, use the following command:
Apart from this SQL Interview Questions Blog, if you want to get trained
from professionals on this technology, you can opt for structured training
from edureka!
You can select unique records from a table by using the DISTINCT
keyword.
There are a lot of ways to fetch characters from a string. For example:
Advantages:
Disadvantage:
Scalar Functions
Inline Table-valued functions
Multi-statement valued functions
Scalar returns the unit, variant defined the return clause. Other two types
of defined functions return table.
Apart from this SQL Interview Questions Blog, if you want to get trained
from professionals on this technology, you can opt for structured training
from edureka!
Local variables:
These variables can be used or exist only inside the function. These
variables are not used or referred by any other function.
Global variables:
These variables are the variables which can be accessed throughout the
program. Global variables cannot be created whenever that function is
called.
Windows mode and Mixed Mode – SQL and Windows. You can go to the
below steps to change authentication mode in SQL Server:
Apart from this SQL Interview Questions Blog, if you want to get trained
from professionals on SQL, you can opt for a structured training from
edureka! Click below to know more.
Got a question for us? Please mention it in the comments section of this
“SQL Interview Questions” blog and we will get back to you as soon as
possible.