Simple Queries in SQL
Simple Queries in SQL
Vidyalayams
Class:XII
Sub: Computer science
Chapter No. : 12
It stores values in fixed lengths and are VARCHAR stores values in variable length
padded with space characters to match along with 1-byte or 2-byte length prefix
the specified length and are not padded with any characters
• Table 12. 1
Accessing database in MySQL
Creating Tables in MySQL
Inserting data into Table
Inserting data into Table
Inserting dates:
Default date format is ‘YYYY-MM-DD’
Making simple queries through select command
Selecting All data:
Making simple queries through select command
Selecting particular Rows:
1. Select all pets with gender(sex) as male(“m”).
mysql> SELECT * FROM pet
WHERE sex = ‘m’;
2. Select all pets that were born on or after Jan 1, 2019
mysql> SELECT * FROM pet
WHERE birth>=‘2019-01-01’;
3. Select all female-dogs.
mysql> SELECT * FROM pet
WHERE species=‘dog’ AND sex=‘f’;
4. Select all snakes or birds.
mysql> SELECT * FROM pet
WHERE species=‘snake’ AND species = ‘bird’;
5. Select all male cats.
mysql> SELECT * FROM pet
WHERE (species=‘cat’ AND sex=‘m’;
Making simple queries through select command
Selecting particular Columns:
1. Display names and birth-dates of all pets.
mysql> SELECT name, birth FROM pet;
2. Displaly owners of pets born after Dec 2018
mysql> SELECT owner FROM pet
WHERE birth>’2018-12-31’;
Making simple queries through select command
Making simple queries through select command
Making simple queries through select command
Making simple queries through select command
Using Column Aliases:
Making simple queries through select command
Making simple queries through select command
Making simple queries through select command
Creating tables with SQL constraints
Table is created using CREATE TABLE commands.
Types of JOINS:
There are many types of JOINS possible,
These are:
(i) Cartesian Product
(ii) Equi Join
(iii) Inner Join
(iv) Natural Join
(v) Left Join
(vi) Right Join
SQL JOINS
(iii)Natural Join :
• A Natural join is a type of equi join where
the join condition compares all the same
names columns in both tables.
SELECT * FROM table1 NATURAL JOIN table2
SQL JOINS
(iv)Natural Join :
SQL JOINS
(iv)Left Join :
It selects rows from both left and right
tables that are matched, plus all rows from
the left table even with no matching rows
found in the right table.
SQL JOINS
(iv)Left Join :
SQL JOINS
(iv)Right Join :
It selects rows from both left and right
tables that are matched, plus all rows from
the right table even with no matching rows
found in the left table.
SQL JOINS
(iv)Right Join :
SQL JOINS
Indexes in database