Sub Query
Sub Query
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 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);
UPDATE employee SET DEPT = 'IT' WHERE EMPID IN (SELECT EMPID FROM employee2 WHERE
EMPID=107);
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.
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.
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:
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.