Page 1

Tutorial: SQL  stands  for  Structured  Query  Language.  It  is  a  very  powerful  and  diverse   language  used  to  access  and  manipulates  database.  Its  loose  syntax  makes  it  easy  to   learn,  but  its  intricate  architecture  may  take  a  bit  longer.   The  most  commonly  used  SQL  command  is  SELECT  statement.  The  SQL  SELECT   statement  is  used  to  query  or  retrieve  data  from  a  table  in  the  database.  A  query   may  retrieve  information  from  specified  columns  or  from  all  of  the  columns  in  the   table.  To  create  a  simple  SQL  SELECT  Statement,  you  must  specify  the  column(s)   name  and  the  table  name.  The  whole  query  is  called  SQL  SELECT  statement.   SELECT  statement  has  two  essential  parts.  The  first  part  is  ”WHAT”,  which   determine  what  we  want  SQL  to  go  and  fetch.  The  second  part  is  “FROM”  and   “WHERE”.  They  identify  where  to  fetch  the  data  from,  which  may  be  from  a  SQL   table  or  SQL  data  object.  SQL  syntax  is  not  case-­‐sensitive,  SELECT  is  the  same  as   select.                                      


-­‐ Basic  SQL  SELECT  Statement  syntax:   SELECT  column_name(s)   FROM  table_name   WHERE  column_name  operator  value    

-­‐SELECT  statement  example:   Given  the  “Person”  table:   P_Id

LastName

FirstName

Address

City

1

Hansen

Ola

Timoteivn 10

Sandnes

2

Svendson

Tove

Borgvn 23

Sandnes

3

Pettersen

Kari

Storgt 20

Stavanger

  Now  we  want  to  select  the  content  of  the  columns  named  “LastName”  and   “FirstName”  from  the  table  above.   We  can  use  the  following  SELECT  statement:   SELECT  LastName,FirstName  FROM  Persons  

The result  will  be:   LastName

FirstName

Hansen

Ola

Svendson

Tove

Pettersen

Kari

         


Now if  we  want  to  select  only  the  persons  living  in  the  city  “Sandnes”  from  the   Person  table,  we  use  the  following  SELECT  statement:   SELECT  *  FROM  Persons   WHERE  City='Sandnes'  

The result  will  look  like  this:   P_Id

LastName

FirstName

Address

City

1

Hansen

Ola

Timoteivn 10

Sandnes

2

Svendson

Tove

Borgvn 23

Sandnes

                                       


-­‐SQL select  all  (*):   "SELECT  (*)"  is  a  shortcut  that  can  be  used  to  select  all  table  columns  rather  than   listing  each  of  them  by  name.  However,  using  it  doesn't  allow  you  to  alter  the   presentation  of  the  results.   The  syntax  is:   SELECT  *  FROM  table  

Now if  we  want  to  select  all  the  columns  from  the  “Person”  table,  we  use  the   following  SELECT  statement:   SELECT  *  FROM  Persons  

And the  result  will  be:   P_Id

LastName

FirstName

Address

City

1

Hansen

Ola

Timoteivn 10

Sandnes

2

Svendson

Tove

Borgvn 23

Sandnes

3

Pettersen

Kari

Storgt 20

Stavanger

   

           

SQL  

SQL Tutorial

Read more
Read more
Similar to
Popular now
Just for you