MY SQL Best Notes For Class 11 and 12
MY SQL Best Notes For Class 11 and 12
If you want to know how many Databases in MySQL then, we need SHOW DATABASES
command.
Syntax—
SHOW DATABASES;
For example:
SHOW DATABASES;
use portal_express ;
For example:
SHOW TABLES;
• The second method is from specific both the column names and the values to inserted.
Syntax--
INSERT INTO <Table name> (column1, column2, column3.....)
VALUES (value1, value2, value3,......);
For example:
For example:
‘2020-07-01’
UPDATE student
SET Class=12, Marks=71
WHERE Name= ‘Rajesh’;
UPDATE student
SET Marks=Marks + 5
WHERE Name= ‘Rajesh’;
• Truncate Statement: -
The MySQL TRUNCATE command is used to delete all the rows from Table and free the space
containing the Table.
Syntax--
TRUNCATE TABLE <table name>;
DESC student;
The SELECT command can perform selection as well as projection. It is most extensively used
MySQL command. The general form of the statement is:-
SELECT what to select
FROM which table
WHERE condition to satisfy;
Before going forward Consider the following table or look the table carefully:-
name species sex birth owner city
Ace DOG M 2018-07-16 RAHUL MUMBAI
Alex BIRD F 2019-09-13 AMIT DELHI
Ally CAT F 2015-01-01 RAVI MUMBAI
Axel DOG F 2012-04-04 KRITI DELHI
Bella CAT F 2013-05-09 JATIN CHENNAI
Bingo SNAKE M 2016-06-30 ABHAY AGRA
Bob DOG M 2020-06-12 NIKHIL DELHI
Chaz SNAKE F 2011-09-13 AMIT RAIPUR
Chevy BIRD F 2012-11-21 KRITI SRINAGAR
Max DOG M 2014-12-24 ELVISH PATANA
Dude CAT M 2015-03-31 GAURAV AGRA
• The NOT IN operator finds rows that do not match in the list. So if you write.
It will list members not from the cities mentioned in the list.
2 = To list names of pets who have names ending with 'y', the command would be:
SELECT name
FROM pet
WHERE name LIKE '%y’;
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
• 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:
ID Last_name First_name
1 Roa Ravi
2 Raj Shahu
3 Kumar Rahul
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.
Or
• The latter way is useful if you want to specify a composite primary key (i.e., having a group of
fields)
For example:-
• Before using the ALTER TABLE command to add a primary key, you'll need to make sure that
the field is defined as 'NOT NULL', in other words, NULL cannot be an accepted value for that
field.
Table: CUSTOMER
Table: ORDERS
Column name Characteristic
Order_ID Primary key
Order_Date
Customer_SID Foreign key
Amount
In the above example, the Customer_SID column in the ORDERS table is a foreign key pointing
to the SID column in the CUSTOMER table.
Just like primary key, Foreign key can also be created in two ways: through CREATE TABLE and
ALTER TABLE commands.
• The above code will designate Customer_SID field of ORDERS table as foreign key referencing
SID field of CUSTOMER table.
SELECT rollno, name, grade, section, marks * 0.35 AS term1 FROM DATA
WHERE marks > 60
ORDER BY section ASC, term1 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.
Many group function accept the following options:
• DISTINCT: - This option causes a group function to consider only distinct value of the
argument expression.
• ALL: - This option cause a group function to consider all values including duplicates.
AVG: -
This function computes the average of given data.
Syntax--
AVG ([DISTINCT | ALL] n);
For example:-
COUNT:-
This function counts the number of rows in a given column or expression.
Syntax--
COUNT ({* [DISTINCT | ALL] expr})
• Return the number of rows in the query.
• If you specify argument expr, this Function returns rows where expr is not null. You can count
either all rows, or only distinct values of expr.
• If you specify the asterisk (*), this function returns all rows, including duplicates and null.
For example:
• Count number of records a table empl.
MAX:-
This function returns the maximum value from a given column or expression.
Syntax--
MAX ([DISTINCT | ALL] expr)
• A Returns maximum value of argument expr.
For example:
• Display maximum salary from table empl.
MIN:-
This function returns the minimum value from a given column or expression.
Syntax--
MIN ( [DISTINCT | ALL] expr)
For example:
• Display the Joining date of senior most employee.
SUM:-
This function returns the sum of values in given column or expression.
Syntax--
SUM([DISTINCT | ALL] n)
• A Returns sum of values of n.
For example:
• Display total salary of all employees listed in table empl.
With Group By clause, you can create groups within groups. Such type of grouping is called
Nested grouping.
This can be done by specifying in GROUP BY expression, where the first field determines the
highest group level, the second field determines the second group level, and so on. The last
field determines the lowest level of grouping.
For example:
The HAVING clause place conditions on groups in contrast to WHERE clause that places
conditions on individual rows. While WHERE conditions can not include aggregate functions,
HAVING condition can do so.
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 -
Example -
SQL join query without any join condition return all the records of joined with all records of
other table .
SQL join query that join two or more tables based on a condition using equality operator.
Syntax -
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 -
Syntax -
Select *
From < table 1 > Natural join < table 2 >
;
The Left join is a particular type of join that selects rows from both left and right tables that
are matched , plus all rows from left table even with no matching rows found in right table .
Syntax -
The Right join is a particular type of join that selects rows from both left and right tables that
are matched , plus all rows from right table even with no matching rows found in left table .
Syntax -
Thankyou !!
Created by -
Path Wala
Computer portal
Portal express