SQL In Simple English

Basic SELECT statements

What are you going to discuss now?
Now you will learn basic SQL statements such as SELECT, INSERT, UPDATE and DELETE.
For all the examples in this article we would be using a sample database table which is shown below

Table Name : people

lastname firstname age address city
Pai
Kiran
22
Mahavir Nagar
Mumbai
Hunter
Jason
41
Oak Street
San Jose
Kanetkar
Yashwant
38
Rajabhai Street
Nagpur


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'.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe