../introduction-to-database-with-sql

Introduction to databse with SQL

Table of contents

What is Database ?

Database is way to orginaze data that you can perform below four operation.

  1. create
  2. read
  3. update
  4. delete

DBMS is a way to interact with database using GUI or textual language

What is SQL

Structred Qurey Language

It is a language which used to interact with database to do that four operation

"Before Doing any change it always recommend to make backup of it"

i am using this repo for sample of database

https://github.com/bradleygrant/sakila-sqlite3

To open your databse,

Type this in you sql enviroment

$ sqllite3 master.db

After that make backup or clone and it will take around 10 min to load your sql

.clone master_backup.db
.open master_backup.db
.read your_sql_table.sql

Once prompt returns,you are ready to write SQL,

Querying

Now we can start our Journey by wrirting some query.

SELECT

To Know what’s inside our database. We use a keyword SELECT which allow to you select some row or all from the table inside the database.

SELECT * FROM 'actor' ;
SELECT "first_name" FROM "actor" ;

output

PENELOPE
NICK
ED
JENNIFER
JOHNNY
BETTE
GRACE
MATTHEW
JOE
CHRISTIAN
.
.
.
THORA

here actor is table in our database and first_name is name of column.

LIMIT

To see from starting to specific amount row inside our database.We use this Keyword LIMIT Which allow to specify the number of rows in qurey output

SELECT "first_name" FROM "actor" LIMIT 10 ;

output

PENELOPE
NICK
ED
JENNIFER
JOHNNY
BETTE
GRACE
MATTHEW
JOE
CHRISTIAN

WHERE

This Keyword WHERE is used to select rows based on condition.It will ouput rows when condition is true

SELECT "postal_code","address" FROM "address" WHERE "postal_code" = 71102 ;

output

71102|1402 Zanzibar Boulevard

Here you can use diffrent operator

for condition =(equal to) !=(not equal to ) <> (not equal to )

SELECT "postal_code", "address" FROM "address" WHERE "postal_code" != 0; 

output

35200|1913 Hanoi Way
17886|1121 Loja Avenue
83579|692 Joliet Street
53561|1566 Inegl Manor
42399|53 Idfu Parkway
.
.
.
27107|1325 Fukuyama Street

here you can replace != with <> the output will be same.

SELECT "postal_code", "address" FROM "address" WHERE "postal_code" <> 0;

output

35200|1913 Hanoi Way
17886|1121 Loja Avenue
83579|692 Joliet Street
53561|1566 Inegl Manor
42399|53 Idfu Parkway
.
.
.
27107|1325 Fukuyama Street
SELECT "postal_code", "address" FROM "address" WHERE NOT "postal_code" = 0;

output

35200|1913 Hanoi Way
17886|1121 Loja Avenue
83579|692 Joliet Street
53561|1566 Inegl Manor
42399|53 Idfu Parkway
.
.
.
27107|1325 Fukuyama Street

To combine condition we can use Keyword AND and OR

SELECT "customer_id", "first_name", "last_name", "store_id", "active" FROM "customer" WHERE "active" = 0 AND "store_id" = 1 ;

output

124|SHEILA|WELLS|1|0
271|PENNY|NEAL|1|0
368|HARRY|ARCE|1|0
406|NATHAN|RUNYON|1|0
482|MAURICE|CRAWLEY|1|0
534|CHRISTIAN|JUNG|1|0
558|JIMMIE|EGGLESTON|1|0
592|TERRANCE|ROUSH|1|0

here i select a customers those who have store_id 1 and they are not active (active = 0)

SELECT "customer_id", "first_name", "last_name", "store_id", "active" FROM "customer" WHERE "active" = 0 OR "store_id" = 1 ;

output

1|MARY|SMITH|1|1
2|PATRICIA|JOHNSON|1|1
3|LINDA|WILLIAMS|1|1
5|ELIZABETH|BROWN|1|1
7|MARIA|MILLER|1|1
10|DOROTHY|TAYLOR|1|1
12|NANCY|THOMAS|1|1
15|HELEN|HARRIS|1|1
16|SANDRA|MARTIN|2|0
17|DONNA|THOMPSON|1|1
.
.
.
598|WADE|DELVALLE|1|1
SELECT "customer_id", "first_name", "last_name", "store_id", "active" FROM "customer" WHERE ("active" = 0 OR "store_id" = 1) AND "first_name" = 'JORDAN';

output

560|JORDAN|ARCHULETA|1|1

Here,paranthese indicate that OR clause should to executed first before AND clause.

NULL

it is possible to have missing data.NULL is a type keyword which can indicate to data that does not have a value or does not exit.

For example i have database of address where some table have missing postal code

SELECT "postal_code","address" FROM "address" WHERE "postal_code" IS NULL;

output

|47 MySakila Drive
|28 MySQL Boulevard
|23 Workhaven Lane
|1411 Lillydale Drive
SELECT "postal_code","address" FROM "address" WHERE "postal_code" IS NOT NULL;

output

35200|1913 Hanoi Way
17886|1121 Loja Avenue
83579|692 Joliet Street
53561|1566 Inegl Manor
42399|53 Idfu Parkway
.
.
.
27107|1325 Fukuyama Street

LIKE

This Keyword is used to select data that roughly matches the specified string.

LIKE is used with operator %(match with given string around them )_(match a single character)

SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE '%love%' ;

output

374|GRAFFITI LOVE
448|IDAHO LOVE
449|IDENTITY LOVER
458|INDIAN LOVE
511|LAWRENCE LOVE
535|LOVE SUICIDES
536|LOVELY JINGLE
537|LOVER TRUMAN
538|LOVERBOY ATTACKS
852|STRANGELOVE DESIRE
SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE 'love%' ;

output

535|LOVE SUICIDES
536|LOVELY JINGLE
537|LOVER TRUMAN
538|LOVERBOY ATTACKS
SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE 'love %' ;

output

535|LOVE SUICIDE
 SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE 'D____________' ;

ouput

203|DAISY MENAGERIE
222|DESERT POSEIDON
233|DISCIPLE MOTHER
237|DIVORCE SHINING
243|DOORS PRESIDENT
247|DOWNHILL ENOUGH
249|DRACULA CRYSTAL
256|DROP WATERFRONT
266|DYNAMITE TARZAN
SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE 'D______________N' ;

output

226|DESTINY SATURDAY
SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE 'w_n_%' ;

output

956|WANDA CHAMBER
976|WIND PHANTOM
977|WINDOW SIDE
983|WON DARES
984|WONDERFUL DROP
985|WONDERLAND CHRISTMAS
986|WONKA SEA
SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE 'w_n_ %' ;

output

976|WIND PHANTOM
SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE '%da__' ;

output

49|BADMAN DAWN
129|CAUSE DATE
328|FOREVER CANDIDATE
386|GUMP DATE
403|HARRY IDAHO
825|SPEAKEASY DATE
890|TIGHTS DAWN
914|TROUBLE DATE
995|YENTL IDAHO
SELECT "film_id",  "title"  FROM "film" WHERE "title" LIKE '% da__' ;

output

49|BADMAN DAWN
129|CAUSE DATE
386|GUMP DATE
825|SPEAKEASY DATE
890|TIGHTS DAWN
914|TROUBLE DATE

Ranges

We can also use the operators <, >, <= and >= in our conditions to match a range of values

SELECT "customer_id","amount", "payment_date"  FROM "payment" WHERE "amount" >=1 AND "amount" <= 3;

output

1|2.99|2005-08-22 19:41:37.000
2|2.99|2005-06-17 20:54:58.000
2|2.99|2005-07-10 06:31:24.000
2|2.99|2005-07-29 12:56:59.000
2|2.99|2005-07-31 21:58:56.000
2|2.99|2005-08-02 13:44:53.000
2|2.99|2005-08-17 03:52:18.000
2|2.99|2005-08-19 06:26:04.000
3|1.99|2005-05-27 17:17:09.000
3|2.99|2005-05-29 22:43:55.000
3|2.99|2005-06-19 08:34:53.000
3|2.99|2005-07-29 11:07:04.000
3|1.99|2005-07-30 13:31:20.000
 SELECT "customer_id","amount", "payment_date"  FROM "payment" WHERE "amount" BETWEEN 1 AND  3;

output

1|2.99|2005-08-22 19:41:37.000
2|2.99|2005-06-17 20:54:58.000
2|2.99|2005-07-10 06:31:24.000
2|2.99|2005-07-29 12:56:59.000
2|2.99|2005-07-31 21:58:56.000
2|2.99|2005-08-02 13:44:53.000
2|2.99|2005-08-17 03:52:18.000
2|2.99|2005-08-19 06:26:04.000
3|1.99|2005-05-27 17:17:09.000
3|2.99|2005-05-29 22:43:55.000
3|2.99|2005-06-19 08:34:53.000
3|2.99|2005-07-29 11:07:04.000
SELECT "customer_id","amount", "payment_date"  FROM "payment" WHERE "amount" > 5;

output

1|5.99|2005-08-22 20:03:46.000
2|6.99|2005-07-10 12:38:56.000
2|5.99|2005-07-27 15:23:02.000
2|5.99|2005-07-27 18:40:20.000
2|5.99|2005-07-29 00:12:59.000
2|5.99|2005-07-29 17:14:29.000
2|10.99|2005-07-30 13:47:43.000
2|6.99|2005-07-30 16:21:13.000
2|6.99|2005-07-30 22:39:53.000
2|5.99|2005-08-02 07:41:41.000
2|6.99|2005-08-02 10:43:48.000
SELECT "customer_id","amount", "payment_id", "rental_id"  FROM "payment" WHERE "amount" >
5 AND "rental_id" > 10000;

output

1|5.99|32|15315
2|5.99|51|11087
2|6.99|52|11177
2|5.99|57|14743
3|5.99|79|10597
3|8.99|81|13403
3|8.99|83|14699
4|8.99|101|12294
4|6.99|104|13807
5|9.99|137|12145
SELECT "customer_id","amount", "payment_id", "rental_id"  FROM "payment" WHERE "amount"<5;

output

1|2.99|1|76
1|0.99|2|573
1|0.99|4|1422
1|4.99|6|1725
1|4.99|7|2308
1|0.99|8|2363
1|3.99|9|3284
1|4.99|12|5244
1|4.99|13|5326

ORDER BY

This keyword allows you to orgainze the returned rows in some specificed order.

 SELECT "customer_id","amount", "payment_id", "rental_id"  FROM "payment" ORDER BY "amount"  LIMIT 10 ;

output

15|0|417|13968
42|0|1178|15407
43|0|1202|15745
53|0|1483|14137
60|0|1671|14741
75|0|2060|14488
75|0|2061|15191
107|0|2902|15497
155|0|4235|12352
163|0|4450|15282
SELECT "customer_id","amount", "payment_id", "rental_id"  FROM "payment" ORDER BY "amount"ASC  LIMIT 10 ;

output

15|0|417|13968
42|0|1178|15407
43|0|1202|15745
53|0|1483|14137
60|0|1671|14741
75|0|2060|14488
75|0|2061|15191
107|0|2902|15497
155|0|4235|12352
163|0|4450|15282
SELECT "customer_id","amount", "payment_id", "rental_id"  FROM "payment" ORDER BY "amount"DESC  LIMIT 10 ;

output

13|11.99|342|8831
116|11.99|3146|14763
195|11.99|5280|16040
196|11.99|5281|106
204|11.99|5550|15415
237|11.99|6409|11479
305|11.99|8272|2166
362|11.99|9803|14759
591|11.99|15821|4383
592|11.99|15850|3973

Aggregate Function

COUNT,AVG,MIN,MAX,and SUM are called aggregate function.and allow us to performe the correspoing operation over multiple rows of data.

 SELECT "title",AVG("length"),"release_year" FROM "film";

output

ACADEMY DINOSAUR|115.272|2006
SELECT "title",ROUND(AVG("length"),2),"release_year" FROM "film";

output

ACADEMY DINOSAUR|115.27|2006
 SELECT "title",ROUND(AVG("length"),3),"release_year" AS "average length" FROM "film"; 

output

ACADEMY DINOSAUR|115.27|2006
SELECT "title",MAX("length"),"release_year" FROM "film";

output

CHICAGO NORTH|185|2006
SELECT "title",MIN("length"),"release_year" FROM "film";

output

ALIEN CENTER|46|2006
 SELECT "title",SUM("length"),"release_year"  FROM "film";

output

ACADEMY DINOSAUR|115272|2006
SELECT COUNT(*) FROM "address" ;

output

603

Remember that we used * to select every row and column from the database. In this case, we are trying to count every row in the database and hence we use the *.

SELECT COUNT("postal_code") FROM "address"

output

599

We observe that the number of postal code is fewer than the number of rows in the database. This is because the COUNT function does not count NULL values.

SELECT COUNT("amount") FROM "payment" ;

output

16049

this query will count the number of payment times values that are not NULL. However, this may include duplicates. Another SQL keyword, DISTINCT, can be used to ensure that only distinct values are counted.

SELECT COUNT(DISTINCT("amount")) FROM "payment" ;

output

19
Go Top