5.3 SQL
5.3 SQL
5.3 SQL
3 STRUCTURED QUERY
LANGUAGE (SQL)
Prepared by: Mohammad Nabeel Arshad
SQL INTRODUCTION:
SQL commands are a set of instructions that are used to interact with the database like Sql
Server, MySql, Oracle etc.
SQL commands are responsible to create and to do all the manipulation on the database.
These are also responsible to give/take out access rights on a particular database.
SQL keywords are NOT case sensitive: select is the same as SELECT
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
SQL COMMANDS, FEATURES, AND FUNCTIONS
TO MANIPULATE DATA:
VALUES
('Hekkan Burger', 'Gateveien 15', 'Sandnes', '4306', 'Norway');
The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
BEGIN TRANSACTION
UPDATE HumanResources.Department
SET Name = 'Information Technology'
WHERE DepartmentID =
The DELETE statement is used to delete rows from a table. If you want to remove a
specific row from a table you should use WHERE condition.
DELETE FROM table_name [WHERE condition];
But if you do not specify the WHERE condition it will remove all the rows from the
table.
DELETE FROM table_name;
Example:
Problem: Delete products over $50.
DELETE Product
WHERE UnitPrice > 50
The following SQL statement returns the cities (only distinct values) from both the "Customers" and the
"Suppliers" table:
Example
JOIN TABLES
Joins allow you to link data from two or more tables together into a single query result--
from one single SELECT statement.
A "Join" can be recognized in a SQL SELECT statement if it has more than one table after
the FROM keyword.
This particular "Join" is known as an "Inner Join" or "Equijoin". This is the most common
type of "Join" that you will see or use.
GROUPING, ORDERING, COUNTING
The GROUP BY Statement in SQL is used to arrange identical data into groups
with the help of some functions. i.e if a particular column has same values in
different rows then it will arrange these rows in a group.
Important Points:
GROUP BY clause is used with the SELECT statement.
In the query, GROUP BY clause is placed after the WHERE clause.
In the query, GROUP BY clause is placed before ORDER BY clause if used any.
SELECT COUNT (CustomerID), Country
FROM Customers
GROUP BY Country;
ORDERING
The SQL COUNT() function returns the number of rows in a table satisfying the
criteria specified in the WHERE clause. It sets the number of rows or non NULL
column values.
COUNT() returns 0 if there were no matching rows
Syntax:
The following statement counts the number of products
SELECT COUNT(ProductID)
FROM Products;
PERFORM QUERIES AND SUBQUERIES
A query is an operation that retrieves data from one or more tables or views. In this
reference, a top-level SELECT statement is called a query, and a query nested
within another SQL statement is called a subquery.