SQL NOTES
SQL NOTES
Syntax--
For example: -
Syntax—
SHOW DATABASES;
For example:
SHOW DATABASES;
Syntax--
For example:
use portal_express ;
Syntax--
DROP DATABASE <database name>;
For example:
Syntax--
For example:
(Name varchar(40),
Roll_no integer,
Marks integer(10),
Class integer);
If you want to see, how many tables has been created in Database, then
we need SHOW TABLES command.
Syntax--
SHOW TABLES;
For example:
SHOW TABLES;
Please make sure that the order of the values is in the same order as the
columns represented in the structure of the table.
Syntax--
Syntax--
INSERT INTO <Table name> (column1, column3, column7.....)
VALUES (value1, value3, value7 ...);
For example:
INSERT INTO student (Rollno, Class, Name, Marks)
VALUES (14, 11, Nilay, 81);
To insert value NULL in specific columns, you can type NULL without
quotes and NULL will be inserted in that column.
Syntax-
For example:
For example:
‘2020-07-01’
Modifying the values in more than one column can be done by separating
the columns along with the new values using SET clause, separated by
commas.
Syntax--
For example:
UPDATE student
SET Class=12, Marks=71
WHERE Name= ‘Rajesh’;
For example:
UPDATE student
SET Class=11
WHERE Name= ‘Ravi’;
Like that: --
For example:
UPDATE student
SET Marks=Marks + 5
WHERE Name= ‘Rajesh’;
Syntax--
For example:
DELETE statement:
DELETE Statement: This command deletes only the rows from the table
based on the condition given in the where clause or deletes all the rows
from the table if no condition is specified. But it does not free the space
containing the table.
Syntax--
For example:
Syntax--
For example:
Syntax--
ALTER TABLE <table name>
MODIFY (<Column name> <datatype>);
For example:
ALTER TABLE student
MODIFY (Name char(100));
Syntax---
ALTER TABLE <table name>
CHANGE <old Column name> <new column name> <datatype [size]>;
For example:
Syntax--
Syntax--
For example:
Before going forward Consider the following table or look the table
carefully:-
Selecting all Data:-
For example
SELECT * FROM pet;
The BETWEEN operator defines a range of values that the column values
must fall in to make the condition true. The range includes both lower
value and the upper value. For example, to list the owner, name whose
pet's date of birth between 1 Jan 2014 to 1 Jan 2019 (both inclusive),
would be:
SELECT owner, name
FROM pet
WHERE birth BETWEEN ‘2014-01-01’ AND ‘2019-01-01’;
You already know that data is added to tables using INSERT INTO
command but INSERT command can also be used to take or derive values
from one table and place them in another by using it with a query.
To do this, simply replace the VALUES clause with an appropriate query as
shown in the following example:
INSERT INTO branch1
SELECT * FROM branch2
WHERE gross > 7000.00;
It will extract all those rows from branch2 that have gross more than
7000.00 and insert this produced result into the table branch1. But for
above command to work, table namely branch1 must be an existing table
of the database.
SQL constraints:-
Some common SQL constraints
SQL NOT NULL constraints:-
By default, a column can hold NULL. If you do not want to allow NULL
value in a column, you will want to place a constraints on this column
specifying that NULL is now not an allowable value.
Look the following statement:
CREATE TABLE customer
(ID integer NOT NULL, First_name varchar (30) NOT NULL, Last_name
varchar (30));
• Column ID & First_name cannot include NULL, while Last_name can
include NULL.
Column ID has a unique constraint, and hence cannot include duplicate values. Such constraint does
not hold for columns Last_Name and First_Name. So, if the table already contains the following
rows:
Will result in an error because the value 3 already exists in the ID column, thus trying to insert
another row with that value violates the UNIQUE constraint.
Just like primary key, Foreign key can also be created in two ways:
through CREATE TABLE and ALTER TABLE commands.
In Create Table command, you can add foreign key's definition through
following syntax:
Foreign Key (<column-to-be-designated-as-foreign-key>) references
Master-Table (<primary-key-of master-table>);
Following example shows how to specify the foreign key when creating
the ORDERS table:
CREATE TABLE ORDERS
(Order_ID integer,
Order_Date date,
Customer_SID integer,
Amount double,
Primary Key (Order ID),
Foreign Key (Customer_SID) references CUSTOMER (SID));
• The above code will designate Customer_SID field of ORDERS table as
foreign key referencing SID field of CUSTOMER table.
In Alter Table command, you can add foreign key's definition through
following syntax :
ALTER TABLE <table-name>
ADD FOREIGN KEY (<column-to-be-designated-as-foreign-key>)
references Master-Table(<primary-key-of master-table>) ;
Following example specifies a foreign key by altering a table. This
assumes that the ORDERS table has been created, and the foreign key
has not yet been put in:
ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER (SID);
For example:
The following statement will sort the records firstly on the column name
Section and then on the basis of descending order of column marks.
SELECT * FROM data
ORDER BY section ASC, marks DESC;
AGGREGATE FUNCTIONS:-
Till now, we have studied about single-row functions which work on a
single value. SQL also provides multiple-row functions which work on
multiple values. So, we can apply SELECT query on a group of records
rather than the entire table. Therefore, these functions are called
Aggregate functions or Group functions.
AVG: -
Syntax--
For example:-
MAX:-
Syntax--
For example:
MIN:-
For example:
SUM:-
Syntax--
SUM([DISTINCT | ALL] n)
For example:
The GROUP BY clause combines all those records that have identical
values in a particular field or a group of fields.
For example:
For example:
For example:
To calculate the average gross and total gross for employees belonging to
‘E4’grade.
SQL JOINS -
SQL join is a query that combines rows from two or more table .
Syntax -
Select <field list>
From <table1>,<table 2>,……
Where <join condition for the tables >
;
Example -
Select ENAME , LOC
From EMPL , DEPT
Where ENAME = “Anoop”
And EMPL.DEPTNO = DEPT.DEPTNO
;
SQL join query without any join condition return all the records of joined
with all records of other table .
Syntax -
Select * from <table 1>,<table 2>…..
;
SQL join query that join two or more tables based on a condition using
equality operator.
Syntax -
Select * from <table 1>,<table 2>
Where < column of table 1 > = < column of table 2 >
;
An inner join implement an equi join . In this join , only those rows are
returned from both the table that satisfy the join condition .
Syntax -
Select <field list>
From <table1> inner join <table 2>
On <join condition for the tables >
;
Syntax -
Select *
From < table 1 > Natural join < table 2 >
;