0% found this document useful (0 votes)
5 views4 pages

Sub Query

The document explains MySQL subqueries, which are nested queries used within other SQL statements, and outlines their rules and advantages. It also discusses the DROP and TRUNCATE commands for deleting database objects, highlighting their differences, and introduces the GRANT and REVOKE commands for managing user permissions in a database. Overall, the document provides a comprehensive overview of important SQL concepts and commands.

Uploaded by

faziluddin90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Sub Query

The document explains MySQL subqueries, which are nested queries used within other SQL statements, and outlines their rules and advantages. It also discusses the DROP and TRUNCATE commands for deleting database objects, highlighting their differences, and introduces the GRANT and REVOKE commands for managing user permissions in a database. Overall, the document provides a comprehensive overview of important SQL concepts and commands.

Uploaded by

faziluddin90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MySQL Subquery

A subquery in MySQL is a query, which is nested into another SQL query and embedded
with SELECT, INSERT, UPDATE or DELETE statement along with the various operators.
We can also nest the subquery with another subquery. A subquery is known as the inner
query, and the query that contains subquery is known as the outer query. The inner query
executed first gives the result to the outer query, and then the main/outer query will be
performed. MySQL allows us to use subquery anywhere, but it must be closed within
parenthesis. All subquery forms and operations supported by the SQL standard will be
supported in MySQL also.

The following are the rules to use subqueries:

●​ Subqueries should always use in parentheses.


●​ If the main query does not have multiple columns for subquery, then a subquery can
have only one column in the SELECT command.
●​ We can use various comparison operators with the subquery, such as >, <, =, IN,
ANY, SOME, and ALL. A multiple-row operator is very useful when the subquery
returns more than one row.
●​ We cannot use the ORDER BY clause in a subquery, although it can be used inside
the main query.
●​ If we use a subquery in a set function, it cannot be immediately enclosed in a set
function.

The following are the advantages of using subqueries:

●​ The subqueries make the queries in a structured form that allows us to isolate each
part of a statement.
●​ The subqueries provide alternative ways to query the data from the table; otherwise,
we need to use complex joins and unions.
●​ The subqueries are more readable than complex join or union statements.

SELECT EMPID,EMP_NAME,DEPT,SALARY
FROM employee WHERE SALARY >
​ (SELECT AVG(SALARY) FROM employee);

SELECT EMP_NAME, DEPT, SALARY FROM employee


WHERE EMPID IN (SELECT EMPID FROM employee2);

SELECT emp_name, city, income FROM employees

WHERE emp_id IN (SELECT emp_id FROM employees);


SELECT * FROM employee
WHERE EMPID IN (SELECT EMPID FROM employee
WHERE SALARY > 75000);

SELECT EMPID, EMP_NAME, Address FROM employee


WHERE EMPID NOT IN (
SELECT EMPID FROM employee2);

SELECT EMPID, EMP_NAME, SALARY FROM employee


WHERE EXISTS (SELECT * FROM employee2
WHERE employee.EMPID = employee2.EMPID);

INSERT INTO employee2 (EMPID, EMP_NAME)


SELECT EMPID, EMP_NAME FROM employee;

UPDATE employee SET DEPT = 'IT' WHERE EMPID IN (SELECT EMPID FROM employee2 WHERE
EMPID=107);

DELETE FROM employee2 WHERE EMPID=ANY(


SELECT EMPID FROM employee);

DROP AND TRUNCATE

ROP in SQL
DROP is an SQL command utilized to erase an object such as a table, database, index, view,
or stored procedure. It could be a DDL (Data Definition Language) statement that's utilized to
remove a schema object from the database. DROP is utilized to remove a table or database
from the database in conjunction with all its related information. DROP could be a permanent
operation and cannot be fixed, so caution should be taken when utilizing the command.
Syntax
DROP TABLE table_name;
Example
DROP TABLE orders;
This command will delete the table called 'orders' from the database. All data stored within
the table will be lost.
TRUNCATE in SQL
Let’s understand in detail what is TRUNCATE in SQL. TRUNCATE could be a SQL
statement that's utilized to erase all the records from a table in a database. It is distinctive
from the Delete statement, which only erases the rows that match an indicated condition.
TRUNCATE is commonly used when deleting huge amounts of information from a database
table because it is faster and more effective than a Delete statement. TRUNCATE, too resets
the table's identity column, in case any, back to its seed value.
Syntax
TRUNCATE TABLE table_name;
Example
TRUNCATE TABLE orders;
This statement will delete all the rows in the 'orders' table and reset the identity column, if
any.

Difference between DROP and TRUNCATE


The following table provides a comprehensive comparison highlighting the difference
between DROP and TRUNCATE in SQL.

DROP TRUNCATE
It completely removes a table from the database. It deletes all the rows from the table.
Rollback is not possible after deletion. Rollback is possible after deletion.
It is a DDL Command. It is a DDL Command.
It is slower than truncate. It is faster than drop.
It can be used with a where clause. It can't be used with a where clause.
It deletes indexes and triggers. It doesn't delete indexes and triggers.

GRANT Command
GRANT, as the name itself suggests, provides. This command allows the administrator to
provide particular privileges or permissions over a database object, such as a table, view, or
procedure. It can provide user access to perform certain database or component operations.
In simple language, the GRANT command allows the user to implement other SQL
commands on the database or its objects. The primary function of the GRANT command in
SQL is to provide administrators the ability to ensure the security and integrity of the data is
maintained in the database.
To have a better understanding of implementing the GRANT statement in the database. Let
us use an example.
Implementing GRANT Statement
Consider a scenario where you are the database administrator, and a student table is in the
database. Suppose you want a specific user Aman to only SELECT (read)/ retrieve the data
from the student table. Then you can use GRANT in the below GRANT statement.

1.​ GRANT SELECT ON student TO Aman;

This command will allow Aman to implement the SELECT queries on the student table. This
will enable the user to read or retrieve information from the student table.
REVOKE Command
As the name suggests, revoke is to take away. The REVOKE command enables the database
administrator to remove the previously provided privileges or permissions from a user over a
database or database object, such as a table, view, or procedure. The REVOKE commands
prevent the user from accessing or performing a specific operation on an element in the
database.
In simple language, the REVOKE command terminates the ability of the user to perform the
mentioned SQL command in the REVOKE query on the database or its component. The
primary reason for implementing the REVOKE query in the database is to ensure the data's
security and integrity.
Let us use an example to better understand how to implement the REVOKE command in
SQL.
Implementing REVOKE Command
Consider a scenario where the user is the database administrator. In the above implementation
of the GRANT command, the user Aman was provided permission to implement a SELECT
query on the student table that allowed Aman to read or retrieve the data from the table. Due
to certain circumstances, the administrator wants to revoke the abovementioned permission.
To do so, the administrator can implement the below REVOKE statement:

1.​ REVOKE SELECT ON student FROM Aman;

This will stop the user Aman from implementing the SELECT query on the student table. The
user may be able to implement other queries in the database.

Benefits of Implementing DCL Commands


There are several advantages of implementing Data Control Language commands in a

You might also like