0% found this document useful (0 votes)
36 views43 pages

SQL Commands and Functions Guide

DROP is a DDL (Data Definition Language) statement that is used to remove or delete objects from the database like tables, indexes, views etc. permanently. Once an object is dropped, all the data and structure of that object is removed from the database and cannot be recovered. DELETE: SQL DELETE statement DELETE is a DML (Data Manipulation Language) statement that is used to remove or delete rows/records from a table but not the table itself. The table structure remains intact but the data is removed. Deleted rows can be recovered using rollback. Key differences: - DROP removes the object definition as well as the data permanently. DELETE only removes rows/records but table structure remains.

Uploaded by

KARTIK SAINI
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)
36 views43 pages

SQL Commands and Functions Guide

DROP is a DDL (Data Definition Language) statement that is used to remove or delete objects from the database like tables, indexes, views etc. permanently. Once an object is dropped, all the data and structure of that object is removed from the database and cannot be recovered. DELETE: SQL DELETE statement DELETE is a DML (Data Manipulation Language) statement that is used to remove or delete rows/records from a table but not the table itself. The table structure remains intact but the data is removed. Deleted rows can be recovered using rollback. Key differences: - DROP removes the object definition as well as the data permanently. DELETE only removes rows/records but table structure remains.

Uploaded by

KARTIK SAINI
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/ 43

PRACTICAL FILE

INFORMATION SYSTEMS MANAGEMENT

Submitted in partial fulfillment of the requirements


for the award of the degree of

BACHELOR OF BUSINESS ADMINISTRATION


To
Guru Gobind Singh Indraprastha University, Delhi

Session 2021-24

SUBMITTED TO : SUBMITTED BY :
Mrs. Komal Arora KARTIK SAINI
(Assistant Professor) BBA 5th SEM
00212401721

DELHI INSTITUTE OF RURAL DEVELOPMENT

(Affiliated to Guru Gobind Singh Indraprastha University)


K-1 GT Karnal Road, Nangli Puna, New Delhi, Delhi 110036

1
INDEX

S.No. Topics PAGE NO.


1 INTRODUCTION TO SQL 3-5

2 TYPES OF SQL COMMANDS 6-13

3 DIFFERNCE BETWEEN DESCRIBE AND SELECT 14-15

4 DIFFERENCE BETWEEN ALTER AND UPDATE 16-17

5 DIFFERENCE BETWEEN DROP AND DELETE 18-19

6 AGGREGATE FUNCTIONS 20-24

7 GROUP BY CLAUSE 25-26

8 ORDER BY CLAUSE 27-31

9 WHERE CLAUSE 32-33

10 HAVING CLAUSE 34

11 DIFFERENCE BETWEEN HAVING AND WHERE 35


CLAUSE

12 PRIMARY KEY, FOREIGN KEY, NOT NULL 36-37

13 LOGICAL OPERATORS ( LIKE 38-42


,AND,OR,BETWEEN,IN)

2
INTRODUCTION
TO SQL

3
What is SQL ?
SQL is a language to operate databases; it includes database creation, deletion, fetching rows,
modifying rows, etc. SQL is an ANSI (American National Standards Institute) standard language, but
there are manydifferent versions of the SQL language. Structured Query Language (SQL) is a
standard computer language for relational database management and data
manipulation. SQL is used to query, insert, update and modify data. Most relational
databases support SQL, which is an added benefit for database administrators (DBAs),
as they are often required to support databases across several different platforms.

First developed in the early 1970s at IBM by Raymond Boyce and Donald
Chamberlin, SQL was commercially released by Relational Software Inc. (now
known as Oracle Corporation) in 1979. The current standard SQL version is voluntary,
vendor-compliant and monitored by the American National Standards Institute (ANSI).
Most major vendors also have proprietary versions that are incorporated and built on
ANSI SQL, e.g., SQL*Plus (Oracle), and Transact SQL (T-SQL) (Microsoft).

One of the most fundamental DBA rites of passage is learning SQL, which begins with
writing the first SELECT statement or SQL script without a graphical user interfaces
(GUI). Increasingly, relational databases use GUIs for easier database management, and
queries can now be simplified with graphical tools, e.g., drag-and-drop wizards.
However, learning SQL is imperative because such tools are never as powerful as SQL.

SQL code is divided into four main categories:


• Queries are performed using the ubiquitous yet familiar SELECT statement,
which is further divided into clauses, including SELECT, FROM, WHERE and
ORDER BY.
• Data Manipulation Language (DML) is used to add, update or delete data and is
actually a SELECT statement subset and is comprised of the INSERT,
DELETE and UPDATE statements, as well as control statements, e.g., BEGIN
TRANSACTION, SAVEPOINT, COMMIT and ROLLBACK.
• Data Definition Language (DDL) is used for managing tables and index
structures. Examples of DDL statements include CREATE, ALTER,
TRUNCATE and DROP.
4
• Data Control Language (DCL) is used to assign and revoke database rights and
permissions. Its main statements are GRANT and REVOKE.

Why SQL?

SQL is widely popular because it offers the following advantages



• Allows users to access data in the relational database management systems.
• Allows users to describe the data.
• Allows users to define the data in a database and manipulate that data.
• Allows to embed within other languages using SQL modules, libraries & pre-
compilers.
• Allows users to create and drop databases and tables.
• Allows users to create view, stored procedure, functions in a database.
• Allows users to set permissions on tables, procedures and views.

5
STRUCTURED QUERY LANGUAGE COMMANDS

DDL DML DQL DCL


DATA DATA DATA QUERY
DATA CONTROL
DEFINITION MANIPULATION LANGUAGE
LANGUAGE
LANGUAGE LANGUAGE

CREATE INSERT SELECT

ALTER UPDATE

DROP DELETE

RENAME

DDL (DATA DEFINITION LANGUAGE):

DDL or Data Definition Language actually consists of the SQL commands that
can be used to define the database schema. It simply deals with descriptions of
the database schema and is used to create and modify the structure of database
objects in the database.

Examples of DDL commands:


● CREATE – is used to create the database or its objects (like table,
index, function, views, store procedure and triggers).
● DROP – is used to delete objects from the database.

● ALTER-is used to alter the structure of the database.

6
DML (DATA MANIPULATION LANGUAGE):

The SQL commands that deals with the manipulation of data present in the
database belong to DML or Data Manipulation Language and this includes
most of the SQL statements.
Examples of DML:
● INSERT – is used to insert data into a table.
● UPDATE – is used to update existing data within a table.
● DELETE – is used to delete records from a database table.

DQL (DATA QUERY LANGUAGE):

DML statements are used for performing queries on the data within schema
objects. The purpose of DQL Command is to get some schema relation based
on the query passed to it.

Example of DQL:
● SELECT – is used to retrieve data from the a database.

DCL (DATA CONTROL LANGUAGE):


DCL includes commands such as GRANT and REVOKE which mainly deals
with the rights, permissions and other controls of the database system.

7
DDL (DATA DEFINTION LAGUAGE):

CREATE QUERY
DATABASE:
SYNTAX:
( create database db1; )

TABLE:
SYNTAX:
( CREATE TABLE table_name (column_name column_type...); )

8
ALTER:
SYNTAX:
( ALTER TABLE table_name , ADD new_column_name column_definition; )

RESULT

DROP:
SYNTAX:
( DROP TABLE table_name; )

9
DROP COLUMN:

DML (DATA MANIPULATION LANGUAGE):


INSERT:
SYNTAX:
( INSERT INTO table_name VALUES ( value1, value2,...valueN ); )

TABLE EMP

TABLE EMP_DEPT

10
UPDATE:
SYNTAX:
{( UPDATE table_name
SET field1=new-value1, field2=new-
value2, ... [WHERE Clause] }

RESULT:

11
DELETE:
SYNTAX:
{ (UPDATE table_name
SET field1=new-value1, field2=new-
value2, ... [WHERE Clause] }

12
DQL (DATA QUERY LANGUAGE):

SELECT:
SYNTAX:
( SELECT * FROM tables [WHERE conditions]; )
13
DIFFERENCE BETWEEN DESCRIBE AND SELECT

DESCRIBE:
SQL DESC statement use for describe the list of column definitions for specified table.
And it isuse for describe a whole specified table’s format.

DESCRIBE statement to get following information:

14
▪ Column Name
▪ Column allow NULL or NOT NULL
▪ Datatype of the Column
▪ With database size precision and If NUMERIC datatype scale.

SYNTAX:

( DESC table name; )

SELECT:

SQL SELECT statement is used to viewing data from the table. This statement return
list of table format table data. Select return the data list from table with format. We
use asterisk (*) sign to fetch all table columns instead of write all column name.
SYNTAX:
( SELECT * FROM table name; )

15
DIFFERENCE BETWEEN ALTER AND UPDATE

ALTER:

16
SQL ALTER TABLE Statement to rename table name, add new column, modify existing column
(data type, size, etc.), drop the table column. SQL ALTER TABLE statement is a
powerful statement to add, manage or update table structure.
SYNTAX:
( ALTER TABLE table name , ADD new column name column definition; )
ALTER : TO ADD NEW COLUMN IN EXISTING EMP TABLE

ALTER: TO RENAME THE EXISTING TABLE

ALTER: TO DROP ONE OF THE COLUMN OF THE TABLE

ALTER: TO MODIFY THE SIZE OF THE DATA

ALTER: TO ADD THE NEW COLUMN

17
UPDATE:
SQL UPDATE statement to only update table records with in database. You can
update all table row or update data only matching conditionally WHERE clause.
SYNTAX:
{ (UPDATE table name
SET field1=new-value1, field2=new-
value2, ... [WHERE Clause] }

UPDATE: TO UPDATE THE NAME

18
DIFFERENCE BETWEEN DROP AND DELETE

DROP:
SQL DROP DATABASE, TABLE statement to drop the existing
database. SYNTAX: ( DROP TABLE table name; )
DROP TABLE:

DROP DATABASE:

19
DELETE:
SYNTAX: ( DELETE FROM table name, where column name = value; )MySQL
DELETE statement is used to delete data from the MySQL table within the database.
By using delete statement, we can delete records on the basis of conditions

20
AGGREGATE FUNCTIONS
In database management an aggregate function is a function where the values of
multiple rows are grouped together as input on certain criteria to form a single value
of more significant meaning.
Various Aggregate Functions:
1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()
6) First()
7) Last()

COUNT:
SYNTAX: ( SELECT COUNT (column name) from
table name; ) Returns total number of records.

21
SUM:
SYNTAX: (SELECT SUM (column name) AS ‘expression’ from table name; )
Sum all Non Null values of Column salary.

AVG:
SYNTAX: ( SELECT AVG (column name) AS ‘expression’ from table name; )
Function is an aggregate function that returns the average value of a group.

MIN:
SYNTAX: ( SELECT MIN(column name) from table name; )
Function returns the smallest value of the selected column.

22
MAX:
SYNTAX: ( SELECT MAX( column name ) from table name; )
Function returns the largest value of the selected column.

FIRST:
SYNTAX: SELECT column_name FROM table_name LIMIT 1;
The MySQL first function is used to return the first value of the selected column. Here, we use
limit clause to select first record or more.
ORIGINAL EMP TABLE:

23
RESULT = LIMIT 1

RESULT = LIMIT 2

24
LAST:
SYNTAX:
(SELECT column_name FROM table_name ORDER BY column_name DESC LIMIT 1;)
MySQL last function is used to return the last value of the selected column.

ORIGINAL EMP TABLE:

RESULT = LIMIT 1

RESULT = LIMIT 2

25
GROUP BY CLAUSE

The GROUP BY statement groups rows that have the same values into summary rows. The GROUP
BY clause is a SQL command that is used to group rows that have the same values. The GROUP BY
clause is used in the SELECT statement. Optionally it is used in conjunction with aggregate
functions to produce summary reports from the database.
SYNTAX:
{ ( SELECT expression1, expression2, ...expression n, Aggregate function (expression)
FROM tables [WHERE conditions]
GROUP BY expression1, expression2, ... expression n; }

ORIGINAL EMP TABLE:

RESULT:

26
ORIGINAL EMP_DEPT TABLE:

RESULT:

27
ORDER BY CLAUSE

The ORDER BY statement in sql is used to sort the fetched data in either ascending or descending
according to one or more columns. By default ORDER BY sorts the data in ascending order. We can
use the keyword DESC to sort the data in descending order and the keyword ASC to sort in
ascending order.
ORDER BY :
SYNTAX:
{ (SELECT expressions
FROM tables [WHERE conditions]
ORDER BY expression [ ASC | DESC ]; }

ORIGINAL EMP TABLE:

RESULT:

28
ORIGINAL EMP_DEPT TABLE:

RESULT:

29
ASCENDING ORDER:
SYNTAX:
{ (SELECT expressions FROM tables [WHERE conditions]
ORDER BY expression [ ASC ]; }

ORIGINAL EMP TABLE:

RESULT:

ORIGINAL EMP_DEPT TABLE:

30
RESULT:

DESCENDING ORDER:
SYNTAX:
{ (SELECT expressions FROM tables [WHERE conditions]
ORDER BY expression [ DESC ]; }

ORIGINAL EMP TABLE:

RESULT:

31
ORIGINAL EMP_DEPT TABLE:

RESULT:

32
WHERE CLAUSE

The SQL WHERE clause is used to specify a condition while fetching the data from a single table or
by joining with multiple tables. If the given condition is satisfied, then only it returns a specific value
from the table. You should use the WHERE clause to filter the records and fetching only the
necessary records. The WHERE clause is used to filter records. The WHERE clause is used to
extract only those records that fulfill a specified condition.

SYNTAX:

( WHERE conditions; )

ORIGINAL EMP TABLE:

RESULT:

33
ORIGINAL EMP_DEPT TABLE:

RESULT:

34
HAVING CLAUSE

SQL - Having Clause. The HAVING Clause enables you to specify conditions that filter which
group results appear in the results. The WHERE clause places conditions on the selected columns,
whereas the HAVING clause places conditions on groups created by the GROUP BY clause.

SYNTAX:
{ SELECT expression1, expression2, ... expression n,
Aggregate Function (expression)
FROM tables
GROUP BY expression1, expression2, ... expression n
HAVING condition; }

RESULT:

35
DIFFERENCE BETWEEN HAVING AND WHERE CLAUSE

“The difference between the having and where clause in SQL is that the where clause cannot be used
with aggregates, but the having clause can.”

1) WHERE clause is processed right after FROM clause in the logical order of query processing,
which means it is processed before GROUP BY clause while HAVING clause is executed after
groups are created.

2) If used in GROUP BY, You can refer any column from a table in WHERE clause but you can
only use columns which are not grouped or aggregated.

3) If you use HAVING clause without group by, it can also refer any column but the index will not
be used as opposed to WHERE clause. For example, the following have the same result set, however
"where" will use the id index and having will do a table scan

select * from table where id = 1


select * from table having id = 1

4) You can use an aggregate function to filter rows with HAVING clause. Because HAVING clause
is processed after the rows have been grouped, you can refer to an aggregate function in the logical
expression. For example, the following query will display only courses which have more than 10
students :

SELECT Course, COUNT(Course) as NumOfStudent from Training GROUP BY Course HAVING


COUNT(COURSE)> 10

5) Another key difference between WHERE and HAVING clause is that WHERE will use Index and
HAVING will not, for example following two queries will produce an identical result but WHERE
will use Index and HAVING will do a table scan

SELECT * FROM Course WHERE Id = 101;


SELECT * FROM Course HAVING Id = 102;

36
PRIMARY KEY

SQL PRIMARY KEY Constraint apply on column(s) for a uniquely identifies each record (row) in
the table.
SQL Primary Key constraint has been specified for certain column. we can not enter duplicate data
in this column.
SQL Primary Key in a table have following three special attributes,
▪ The NOT NULL attribute is automatic active.
▪ The data across the column must be unique.
▪ Defines column as a mandatory column.

FOREIGN KEY
SQL FOREIGN KEY Constraint apply on column(s) for whose value must have reference in another
table column (that existing column must be primary key or unique key constraint).SQL FOREIGN
KEY constraints also known as relationship (referential) constraints.
SYNTAX:
FOREIGN KEY (table2.column name) REFERENCES table1(column name)
TABLES:

37
NOT NULL
SQL NOT NULL Constraint enforces to a column is always contain a value. This means that you
can not insert NULL (blank) value in this field. SQL NOT NULL Constraint applied only at column
level. You should manually define NOT NULL constraint because table column set NULL value
already.
SYNTAX: ( column name data type data size NOT NULL, )
( Create table table name (column name data size Not Null)

38
LOGICAL OPERATOR

LIKE
SQL LIKE operator is used with WHERE clause to matches specific pattern in a column. SQL LIKE
condition apply on table column data.
Following two wildcards are often used with the LIKE operator

▪ % - Represents zero, or any number of characters


▪ _ - Represents a single character

SYNTAX:

( SELECT * FROM table name WHERE column name LIKE 'value';

ORIGINAL EMP TABLE:

RESULT:

39
BETWEEN
SQL BETWEEN operator used for fetching within range data. SQL BETWEEN query simply a
shorthand way of expressing an inclusive range comparison.
SYNTAX:
( SELECT * FROM table name WHERE column name BETWEEN value AND value; )
ORIGINAL EMP TABLE:

RESULT:

40
AND
SQL AND condition use to test more then one conditions in INSERT, UPDATE, DELETE, SELECT
statement. AND operator work is test first condition if true come to a second and so forth, otherwise not check
next condition.
SYNTAX:

(SELECT * FROM table name WHERE Column name conditon 'value' AND Column
name condition ‘value’; )

ORIGINAL EMP TABLE:

RESULT:

41
OR

SQL OR Condition use to test more then one conditions in INSERT, UPDATE, DELETE, SELECT
statement. OR operator test all condition even if condition TRUE or FALSE. And return data when
any one of the condition TRUE. SQL OR Operator same as AND operator, return the record base
filtered data. INSERT, UPDATE, DELETE, SELECT statement perform only one of the specified
condition TRUE.
SYNTAX:
(SELECT * FROM table name WHERE Column name conditon 'value' OR Column
name condition ‘value’; )

ORIGINAL EMP TABLE:

RESULT:

42
IN
The MySQL IN condition is used to reduce the use of multiple OR conditions in a SELECT,
INSERT, UPDATE and DELETE statement.
SYNTAX:

SELECT * FROM table name WHERE Column name IN ('values’);

ORIGINAL EMP TABLE:

RESULT:

43

You might also like