SQL Interview
SQL Interview
c) DCL (Data Control Language): These statements are used to set privileges such
as GRANT and REVOKE database access permission to the specific user.
Q #4) How do we use the DISTINCT statement? What is its use?
Answer: The DISTINCT statement is used with the SELECT statement. If the record
contains duplicate values then the DISTINCT statement is used to select different
values among duplicate records.
Syntax:
SELECT DISTINCT column_name(s)
FROM table_name;
Q #5) What are the different Clauses used in SQL?
Answer:
WHERE Clause: This clause is used to define the condition, and extract and display
only those records which fulfill the given condition.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition;
GROUP BY Clause: It is used with the SELECT statement to group the result of the
executed query using the value specified in it. It matches the value with the column
name in tables and groups the end result accordingly.
Further reading => MySQL Group By Tutorial
Syntax:
SELECT column_name(s)
FROM table_name
GROUP BY column_name;
HAVING clause: This clause is used in association with the GROUP BY clause. It is
applied to each group of results or the entire result as a single group. It is much
similar to the WHERE clause but the only difference is you cannot use it without the
GROUP BY clause
Syntax:
SELECT column_name(s)
FROM table_name
GROUP BY column_name
HAVING condition;
ORDER BY clause: This clause is used to define the order of the query output either
in ascending (ASC) or in descending (DESC). Ascending (ASC) is set as the default
one but descending (DESC) is set explicitly.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
ORDER BY column_name ASC|DESC;
USING clause: USING clause comes in use while working with SQL JOIN. It is used
to check equality based on columns when tables are joined. It can be used instead of
the ON clause in JOIN.
Syntax:
SELECT column_name(s)
FROM table_name
JOIN table_name
USING (column_name);
Q #6) Why do we use SQL constraints? Which constraints we can use while
creating a database in SQL?
Answer: Constraints are used to set the rules for all records in the table. If any
constraints get violated then it can abort the action that caused it.
Constraints are defined while creating the database itself with the CREATE TABLE
statement or even after the table is created once with the ALTER TABLE statement.
4 major types of Joins are used while working on multiple tables in SQL databases:
INNER JOIN: It is also known as SIMPLE JOIN which returns all rows from BOTH
tables when it has at least one matching column.
Syntax:
SELECT column_name(s)
FROM table_name1 
INNER JOIN table_name2
ON column_name1=column_name2;
For Example,
In this example, we have a table Employee with the following data:
The second table’s name is Joining.
RIGHT JOIN (RIGHT OUTER JOIN): This joins returns all rows from the RIGHT
table and its matched rows from the LEFT table.
Syntax:
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON column_name1=column_name2;
For Example,
In this example, we have a table Employee with the following data:
Syntax:
CREATE TRIGGER name {BEFORE|AFTER} (event [OR..]}
ON table_name [FOR [EACH] {ROW|STATEMENT}]
EXECUTE PROCEDURE functionname {arguments}
Q #13) What is View in SQL?
Answer: A View can be defined as a virtual table that contains rows and columns
with fields from one or more tables.
Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Q #14) How we can update the view?
Answer: SQL CREATE and REPLACE can be used for updating the view.
Execute the below query to update the created view.
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Q #15) Explain the working of SQL Privileges.
Answer: SQL GRANT and REVOKE commands are used to implement privileges in
SQL multiple user environments. The administrator of the database can grant or
revoke privileges to or from users of database objects by using commands like
SELECT, INSERT, UPDATE, DELETE, ALL, etc.
GRANT Command: This command is used to provide database access to users
other than the administrator.
Syntax:
GRANT privilege_name
ON object_name
TO {user_name|PUBLIC|role_name}
[WITH GRANT OPTION];
In the above syntax, the GRANT option indicates that the user can grant access to
another user too.