0% found this document useful (0 votes)
4 views

SQL Commands

Uploaded by

yashoda sailwal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL Commands

Uploaded by

yashoda sailwal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Introduction to SQL Commands

CA Department

Course Coordinator:- Course Name:- BCA III Sem


Name- Shruti Merothia Course Code:- CA 213

Designation- Assistant Professor


Mail Id : [email protected]
Objectives
1. Understand the purpose of SQL commands in managing and
manipulating database data.

2. Identify and differentiate between the types of SQL commands:


DDL, DML, DQL, DCL, and TCL.

3. Learn how to retrieve data from a database using Data Query


Language (DQL).

4. Understand how to control user access and permissions with Data


Control Language (DCL) commands.

5. Gain knowledge of Transaction Control Language (TCL) to


manage transactions and ensure data integrity.
Introduction
1. Introduction to SQL Commands
2. Types of SQL Commands
3. Data Query Language
4. Data Control Language
5. Transaction Control Language
6. MCQs
Introduction to SQL Commands
SQL commands are instructions. It is used to communicate
with the database. It is also used to perform specific tasks,
functions, and queries of data.
SQL can perform various tasks like create a table, add data to
tables, drop the table, modify the table, set permission for
users.
Types of SQL Commands:
There are five types of SQL commands: DDL, DML, DCL, TCL,
and DQL.
Data Query Language (DQL)

DQL is a portion of a SQL statement that allows you to get and


organize data from a database. You can use the SELECT command to
extract data from a database in order to perform actions on it.

Command Description Syntax

SELECT column1,
It is used to retrieve
column2, ...FROM
SELECT data from the
table_name WHERE
database
condition;
-- DQL (Data Query Language)

-- 1. SELECT: Retrieves data from one or more tables


SELECT column1, column2, ...
FROM table_name
WHERE condition;

-- Example:
SELECT * FROM customers;
SELECT first_name, last_name FROM customers WHERE city = 'New
York';
-- 2. COUNT: Counts the number of rows in a table or a specific
column
SELECT COUNT(*) FROM orders;
SELECT COUNT(DISTINCT product_id) FROM order_items;

-- 3. SUM: Calculates the sum of values in a column


SELECT SUM(price) FROM orders;

-- 4. AVG: Calculates the average value of a column


SELECT AVG(age) FROM employees;
5. MIN: Finds the minimum value in a column

SELECT MIN(salary) FROM employees;

6. MAX: Finds the maximum value in a column

SELECT MAX(price) FROM products;


Data Control Language(DCL)
DCL commands are used to grant and take back authority from
any database user.
Here are some commands that come under DCL:
•GRANT
•REVOKE
Command Description Syntax

Assigns new privileges to GRANT privilege_type


a user account, allowing [(column_list)] ON
GRANT access to specific [object_type] object_name
database objects, actions, TO user [WITH GRANT
or functions. OPTION];

Removes previously REVOKE [GRANT


granted privileges from a OPTION FOR]
user account, taking away privilege_type
REVOKE
their access to certain [(column_list)] ON
database objects or [object_type] object_name
actions. FROM user [CASCADE];
Terminology
privilege: The specific permission you want to grant (e.g., SELECT,
INSERT, UPDATE, DELETE, EXECUTE).

object: The database object (e.g., table, view, procedure).

user/role/PUBLIC: The recipient(s) of the privileges. PUBLIC


means all users.

WITH GRANT OPTION: This allows the recipient to grant the


same privileges to others.
Example:

1.GRANT SELECT, INSERT ON employees TO john;

These grants SELECT and INSERT privileges on the employees table


to user john.

2. GRANT SELECT, INSERT ON employees TO john;

These grants SELECT and INSERT privileges on the employees table


to user john.
Example:

3. GRANT SELECT ON employees TO john WITH GRANT OPTION;

This grants SELECT privilege to john and allows him to grant it to


others.

4.REVOKE SELECT, INSERT ON employees FROM john;

This revokes all privileges on the employees table from user john.
Example:

5. REVOKE ALL ON employees FROM john;

This revokes all privileges on the employees table from user john.
Transaction Control Language(TCL)
TCL commands can only use with DML commands like INSERT,
DELETE and UPDATE only.
Here are some commands that come under TCL:
•COMMIT
•ROLLBACK
•SAVEPOINT
Command Description Syntax

Saves all changes


COMMIT made during the COMMIT;
transaction

Undo all the changes


ROLLBACK made during the ROLLBACK;
transaction

Creates a savepoint
SAVEPOINT within the current SAVEPOINT
transaction
MCQs

1. DCL commands are primarily used for:


A) Querying data
B) Managing database transactions
C) Managing user permissions and access rights
D) Defining table structures

2. Which of the following is a DCL command in SQL?


A) GRANT
B) SELECT
C) UPDATE
D) COMMIT
3. What does the REVOKE command do in SQL?
A) Grants permissions to a user
B) Removes permissions from a user
C) Deletes a record from a table
D) Ends a transaction

4. The GRANT command is used to:


A) Define data types
B) Grant specific permissions to users
C) Commit a transaction
D) Remove a table
5. Which command would you use to prevent a user from accessing a
database object?
A) REVOKE
B) GRANT
C) COMMIT
D) SELECT

6. Which SQL command is considered a DQL command?


A) INSERT
B) SELECT
C) UPDATE
D) DELETE
7. What does the SELECT command do in SQL?
A) Inserts data into a table
B) Modifies a table structure
C) Retrieves data from a table
D) Grants permissions

8. DQL commands are primarily used for:


A) Managing user permissions
B) Retrieving data from the database
C) Creating database tables
D) Controlling transactions
9. The clause used with SELECT to filter results based on conditions
is:
A) WHERE
B) GROUP BY
C) ORDER BY
D) JOIN

10. Which clause would you use with SELECT to retrieve unique
records only?
A) WHERE
B) DISTINCT
C) GROUP BY
D) JOIN
11. What does the COMMIT command do in SQL?
A) Removes user permissions
B) Ends a transaction and saves all changes
C) Rolls back all changes to the last save point
D) Deletes data from a table

12. Which TCL command undoes changes in a transaction before it is


committed?
A) SAVEPOINT
B) COMMIT
C) ROLLBACK
D) GRANT
13. The purpose of a SAVEPOINT command is to:
A) Grant permissions to a user
B) Create a temporary save point within a transaction
C) Retrieve data from a table
D) Update multiple records at once

14. Which command would you use to partially roll back a transaction
to a specific point?
A) ROLLBACK TO SAVEPOINT
B) COMMIT TO SAVEPOINT
C) REVOKE
D) DELETE
15. In SQL, which command is used to save all changes made in a
transaction to the database?
A) REVOKE
B) COMMIT
C) SAVEPOINT
D) ROLLBACK

16. What happens if a transaction is not committed in SQL?


A) Changes are saved automatically
B) Changes remain temporary until committed
C) The transaction is ignored
D) Only half the data is saved
Learning Outcomes
1. Explain the purpose of SQL commands in database management.

2. Identify and categorize SQL commands into DDL, DML, DQL,


DCL, and TCL.

3. Demonstrate data retrieval techniques using the SELECT


command from DQL.

4. Apply DCL commands to control user access and permissions in a


database.

5. Use TCL commands, such as COMMIT and ROLLBACK, to


manage transactions effectively and maintain data integrity.
References:
1.SQL in 10 Minutes, Sams Teach Yourself by Ben Forta
2.SQL for Data Analysis by Cathy Tanimura
3.Learning SQL by Alan Beaulieu
4.Head First SQL by Lynn Beighley
5.SQL Cookbook by Anthony Molinaro
THANK YOU

You might also like