Ism Lab File
Ism Lab File
Ism Lab File
GHAZIABAD
COLLEGE CODE – 247
AFFILIATED TO GURU GOBIND SINGH INDRAPRASTHA
UNIVERSITY
DELHI – 110078
SUBMITTED TO SUBMITTED BY
MRS. SWEETY TYAGI AFSANA
BBA-III YEAR
SQL Introduction:-
What is SQL?
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.
To build a web site that shows data from a database, you will need:
RDBMS
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.
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-
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
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:
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 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-
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).
Example-
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
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.
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
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 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-
Example-
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:
The following SQL statement inserts a new record in the "Customers" table:
Example-
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
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.
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-
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
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 following SQL statement deletes the customer "Alfreds Futterkiste" from the
"Customers" table:
Example
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:
The following SQL statement deletes all rows in the "Customers" table, without deleting the
table:
Example
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.
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;
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
The following SQL statement selects the first 50% of the records from the "Customers" table
(for SQL Server/MS Access):
Example
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;
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
COUNT() Example
Example
Note: NULL values are not counted.
AVG() Example
The following SQL statement finds the average price of all products:
Example
SUM() Example
The following SQL statement finds the sum of the "Quantity" fields in the "OrderDetails"
table:
Example
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 and the underscore can also be used in combinations!
LIKE Syntax
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
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"
The following SQL statement selects all customers with a CustomerName starting with "a":
Example
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
* Represents zero or more characters bl* finds bl, black, blue, and blob
[] 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 any single numeric 2#5 finds 205, 215, 225, 235, 245, 255, 265, 275, 285, and 295
character
% Represents zero or more characters bl% finds bl, black, blue, and blob
[] 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
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
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"
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
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 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
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
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.
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