Ism Lab File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

SGIT SCHOOL OF MANAGEMENT

GHAZIABAD
COLLEGE CODE – 247
AFFILIATED TO GURU GOBIND SINGH INDRAPRASTHA
UNIVERSITY
DELHI – 110078

INFORMATION SYSTEM MANAGEMENT


LAB (BBA – 307)
BBA – III YEAR

SUBMITTED TO SUBMITTED BY
MRS. SWEETY TYAGI AFSANA
BBA-III YEAR
SQL Introduction:-
What is SQL?

 SQL stands for Structured Query Language


 SQL lets you access and manipulate databases
 SQL became a standard of the American National Standards Institute (ANSI) in 1986,
and of the International Organization for Standardization (ISO) in 1987

What Can SQL do?

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views

SQL is a Standard - BUT....

Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.

However, to be compliant with the ANSI standard, they all support at least the major
commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

Using SQL in Your Web Site

To build a web site that shows data from a database, you will need:

 An RDBMS database program (i.e. MS Access, SQL Server, MySQL)


 To use a server-side scripting language, like PHP or ASP
 To use SQL to get the data you want
 To use HTML / CSS to style the page

RDBMS

RDBMS stands for Relational Database Management System.


RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server,
IBM DB2, Oracle, MySQL, and Microsoft Access.

The data in RDBMS is stored in database objects called tables. A table is a collection of
related data entries and it consists of columns and rows.

Look at the "Customers" table:

Example-

Every table is broken up into smaller entities called fields. The fields in the Customers table
consist of CustomerID, CustomerName, ContactName, Address, City, PostalCode and
Country. A field is a column in a table that is designed to maintain specific information
about every record in the table.

A record, also called a row, is each individual entry that exists in a table. For example, there
are 91 records in the above Customers table. A record is a horizontal entity in a table.

A column is a vertical entity in a table that contains all information associated with a specific
field in a table.

SQL Syntax:-
Database Tables

A database most often contains one or more tables. Each table is identified by a name (e.g.
"Customers" or "Orders"). Tables contain records (rows) with data.

In this tutorial we will use the well-known Northwind sample database (included in MS
Access and MS SQL Server).
The following SQL statement selects all the records in the "Customers" table:

Example-

Semicolon after SQL Statements?

Some database systems require a semicolon at the end of each SQL statement.

Semicolon is the standard way to separate each SQL statement in database systems that
allow more than one SQL statement to be executed in the same call to the server.

SQL Select:-
The SQL SELECT Statement

The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.

SELECT Syntax
SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If
you want to select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;


SELECT Column Example

The following SQL statement selects the "CustomerName" and "City" columns from the
"Customers" table:
Example-

SELECT * Example

The following SQL statement selects all the columns from the "Customers" table:

Example-

The SQL SELECT DISTINCT Statement

The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values; and sometimes you only want
to list the different (distinct) values.
SELECT DISTINCT Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
The following SQL statement selects only the DISTINCT values from the "Country" column
in the "Customers" table:
Example-

The following SQL statement lists the number of different (distinct) customer countries:

Example
Here is the workaround for MS Access:

Example-

The SQL WHERE Clause

The WHERE clause is used to filter records.

It is used to extract only those records that fulfill a specified condition.

WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example-
Text Fields vs. Numeric Fields

SQL requires single quotes around text values (most database systems will also allow double
quotes).

However, numeric fields should not be enclosed in quotes:

Example-

Operators in The WHERE Clause


Operator Description

= Equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

<> Not equal. Note: In some versions of SQL this operator may be written as !=

BETWEEN Between a certain range

LIKE Search for a pattern

IN To specify multiple possible values for a column

The following operators can be used in the WHERE clause:

The SQL AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:
 The AND operator displays a record if all the conditions separated by AND are
TRUE.
 The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
AND Example

The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city is "Berlin":

Example

OR Example
The following SQL statement selects all fields from "Customers" where city is "Berlin" OR
"München":

Example-

NOT Example

The following SQL statement selects all fields from "Customers" where country is NOT
"Germany":
Example

Combining AND, OR and NOT

You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city must be "Berlin" OR "München" (use parenthesis to form complex
expressions):

Example-
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany" and NOT "USA":

Example-

The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in descending order, use the DESC keyword.

ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
ORDER BY Example

The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" column:

Example-
ORDER BY DESC Example

The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:

Example-

ORDER BY Several Columns Example


The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column. This means that it orders by Country, but if
some rows have the same Country, it orders them by CustomerName:

Example-

The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to specify the
column names in the SQL query. However, make sure the order of the values is in the same
order as the columns in the table. Here, the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
INSERT INTO Example

The following SQL statement inserts a new record in the "Customers" table:
Example-

Insert Data Only in Specified Columns

It is also possible to only insert data in specific columns.

The following SQL statement will insert a new record, but only insert data in the
"CustomerName", "City", and "Country" columns (CustomerID will be updated
automatically):

Example
SQL NULL Value

What is a NULL Value?

A field with a NULL value is a field with no value.

If a field in a table is optional, it is possible to insert a new record or update a record without
adding a value to this field. Then, the field will be saved with a NULL value.

How to Test for NULL Values?

It is not possible to test for NULL values with comparison operators, such as =, <, or <>.

We will have to use the IS NULL and IS NOT NULL operators instead.

IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
The IS NULL Operator

The IS NULL operator is used to test for empty values (NULL values).

The following SQL lists all customers with a NULL value in the "Address" field:

Example-
The IS NOT NULL Operator

The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).

The following SQL lists all customers with a value in the "Address" field:

Example-

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Table

The following SQL statement updates the first customer (CustomerID = 1) with a new
contact person and a new city.

Example

UPDATE Multiple Records

It is the WHERE clause that determines how many records will be updated.

The following SQL statement will update the ContactName to "Juan" for all records where
country is "Mexico":

Example-
Update Warning!

Be careful when updating records. If you omit the WHERE clause, ALL records will be
updated!

The SQL DELETE Statement

The DELETE statement is used to delete existing records in a table.


DELETE Syntax
DELETE FROM table_name WHERE condition;
SQL DELETE Example

The following SQL statement deletes the customer "Alfreds Futterkiste" from the
"Customers" table:

Example

Delete All Records

It is possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will be intact:

DELETE FROM table_name;

The following SQL statement deletes all rows in the "Customers" table, without deleting the
table:
Example

The SQL SELECT TOP Clause

The SELECT TOP clause is used to specify the number of records to return.

The SELECT TOP clause is useful on large tables with thousands of records. Returning a
large number of records can impact performance.

SQL Server / MS Access Syntax:

SELECT TOP number|percent column_name(s)


FROM table_name
WHERE condition;

MySQL Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;

Oracle 12 Syntax:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY;

Older Oracle Syntax:


SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

Older Oracle Syntax (with ORDER BY):

SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number;
SQL TOP, LIMIT and FETCH FIRST Examples

The following SQL statement selects the first three records from the "Customers" table (for
SQL Server/MS Access):

Example

SQL TOP PERCENT Example

The following SQL statement selects the first 50% of the records from the "Customers" table
(for SQL Server/MS Access):
Example

ADD a WHERE CLAUSE

The following SQL statement selects the first three records from the "Customers" table,
where the country is "Germany" (for SQL Server/MS Access):

Example
The SQL MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
MIN() Example

The following SQL statement finds the price of the cheapest product:

Example

MAX() Example

The following SQL statement finds the price of the most expensive product:

Example
The SQL COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that matches a specified criterion.

COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;

The AVG() function returns the average value of a numeric column.

AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;

The SUM() function returns the total sum of a numeric column.

SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
COUNT() Example

The following SQL statement finds the number of products:

Example
Note: NULL values are not counted.

AVG() Example

The following SQL statement finds the average price of all products:

Example

Note: NULL values are ignored.

SUM() Example

The following SQL statement finds the sum of the "Quantity" fields in the "OrderDetails"
table:
Example

The SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

 The percent sign (%) represents zero, one, or multiple characters


 The underscore sign (_) represents one, single character

The percent sign and the underscore can also be used in combinations!

LIKE Syntax

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

Here are some examples showing different LIKE operators with '%' and '_' wildcards:

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or Finds any values that have "or" in any position
%'

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length

WHERE CustomerName LIKE 'a__ Finds any values that start with "a" and are at least 3 characters in length
%'

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

SQL LIKE Examples

The following SQL statement selects all customers with a CustomerName starting with "a":

Example

SQL Wildcard Characters

A wildcard character is used to substitute one or more characters in a string.

Wildcard characters are used with the LIKE operator. The LIKE operator is used in
a WHERE clause to search for a specified pattern in a column.
Wildcard Characters in MS Access

Symbol Description Example

* Represents zero or more characters bl* finds bl, black, blue, and blob

? Represents a single character h?t finds hot, hat, and hit

[] Represents any single character within h[oa]t finds hot and hat, but not hit
the brackets

! Represents any character not in the h[!oa]t finds hit, but not hot and hat
brackets

- Represents a range of characters c[a-b]t finds cat and cbt

# Represents any single numeric 2#5 finds 205, 215, 225, 235, 245, 255, 265, 275, 285, and 295
character

Wildcard Characters in SQL Server

Symbol Description Example

% Represents zero or more characters bl% finds bl, black, blue, and blob

_ Represents a single character h_t finds hot, hat, and hit

[] Represents any single character within the h[oa]t finds hot and hat, but not hit
brackets
^ Represents any character not in the brackets h[^oa]t finds hit, but not hot and hat

- Represents a range of characters c[a-b]t finds cat and cbt

All the wildcards can also be used in combinations!

Here are some examples showing different LIKE operators with '%' and '_' wildcards:

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"

WHERE CustomerName LIKE '%a' Finds any values that ends with "a"

WHERE CustomerName LIKE '%or Finds any values that have "or" in any position
%'

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a__%' Finds any values that starts with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and ends with "o"

Using the % Wildcard

The following SQL statement selects all customers with a City starting with "ber":

Example
The following SQL statement selects all customers with a City containing the pattern "es":

Example

The SQL IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.


IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

or:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
IN Operator Examples

The following SQL statement selects all customers that are located in "Germany", "France"
or "UK":

Example

The following SQL statement selects all customers that are NOT located in "Germany",
"France" or "UK":
Example

The SQL BETWEEN Operator

The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
BETWEEN Example

The following SQL statement selects all products with a price between 10 and 20:

Example
NOT BETWEEN Example

To display the products outside the range of the previous example, use NOT BETWEEN:

Example

BETWEEN with IN Example

The following SQL statement selects all products with a price between 10 and 20. In
addition; do not show products with a CategoryID of 1,2, or 3:
Example

BETWEEN Text Values Example

The following SQL statement selects all products with a ProductName between Carnarvon
Tigers and Mozzarella di Giovanni:

Example

SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.

Aliases are often used to make column names more readable.

An alias only exists for the duration of that query.

An alias is created with the AS keyword.

Alias Column Syntax


SELECT column_name AS alias_name
FROM table_name;
Alias Table Syntax
SELECT column_name(s)
FROM table_name AS alias_name;
Alias for Columns Examples

The following SQL statement creates two aliases, one for the CustomerID column and one
for the CustomerName column:

Example

The following SQL statement creates an alias named "Address" that combine four columns
(Address, PostalCode, City and Country):
Example

SQL JOIN

A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.

Example

You might also like