|
|
|
SQL In Simple English - Part I
|
Author: Kiran Pai |
|
|
|
|
What is SELECT statement? How do I use it?
The SELECT statement lets you
select a set of values from a table in a database. The values
selected from the database table would depend on the various
conditions that are specified in the SQL query. Here are
some ways to use the SELECT statement. I have listed the
SQL statements and the respective results that you would
obtain if you would execute those queries.
SELECT
lastname,firstname FROM people
Would return a recordset with 3 records. Each record would
have 2 values.
The first record would have 2 values 'Pai' and 'Kiran'.
Whereas the second record would have 2 values 'Hunter' and
'Jason'.
SELECT * FROM people WHERE firstname='Jason'
Would return a recordset with 1 record. The record
would have 5 values in that, namely 'Hunter' , 'Jason' ,
'41' , 'Oak Street' and 'San Jose'.
SELECT * FROM people WHERE age>25
Would return a recordset with 2 records.
Note : That whenever you are comparing a varchar
the value should be enclosed in single inverted commas (
' ). Whereas when you compare an integer the value need
not be enclosed in single inverted commas.
|
|
|
|
|
|
How can I compare a part of the name rather than the entire
name?
SELECT * FROM people WHERE firstname
LIKE '%an%'
Would return a recordset with 2 records. This statement
would return 2 records since the sequence 'an'
occurs in 2 firstname values, 'Kiran'
and 'Yashwant'.
|
|
|
|
|
|
Can I use Boolean operators such as AND or OR to make
complex queries?
Good news!! Yes you can.. Actually as a matter of fact,
once you start developing professional database applications
you would almost always use such Boolean operators to make
effective queries.
SELECT address FROM people WHERE (firstname='Kiran'
OR city='Nagpur') AND age>30
Would return a recordset consisting of 1 record with 1 value
only. Since AND condition specifies that the firstname
of the person could be 'Kiran' or his city could
be 'Nagpur' , BUT that person has to be over the age
of 30. The recordset would have only 1 value in it : 'Rajabhai
Street'.
|
|
|
|
|
|
What is the INSERT statement? How do I use it?
The INSERT statement lets you
insert information into a database. A few examples are shown
below
INSERT
INTO people VALUES ('Bush', 'George', 47 , 'White House',
'Washington')
Would
insert a new row at the bottom of the table people
consisting of the values in parentheses in the above statement.
INSERT INTO people (lastname, city)
VALUES ('Gates', 'Redmond')
Would insert a new row at the bottom of the table
people consisting of only 2 values as present in the above
statement, namely 'Gates' and 'Redmond'. The remaining columns
for that particular record would be left empty (null).
Note : A null value is different from 0 or ''(Empty
String). A perfect example of this would be a column describing
the hair colour for many people. In case the person is bald
then the value of the colour should be null rather than
empty. This would be perfect from the database design view.
A particular entity which doesn't exist should be represented
similarly and not by empty Strings.
|
|
|
|
|
|
|
|
|