Showing posts with label select. Show all posts
Showing posts with label select. Show all posts

27 April 2013

more SQL tutorial



Let us build a database  'test' with table  'person' inside it, having column  name, age, gender. see how to do this


Insert a line into table:
INSERT INTO person (name, gender) VALUES ('omphu', 'female')
This will add a new line or data into the table  'person' on column  name, gender with value omphu, female respectively and will leave the value of age blank.

Select line using WHERE:
SELECT * FROM person WHERE gender='female'

Select specific column:
SELECT name FROM person WHERE gender='male'
This will output the value of column name where gender is female.

Specify more argument to refine search:
SELECT name, age FROM person WHERE age='21' AND gender='male'
Will output result if this two condition are true,  age & gender.

SELECT name, age FROM person where age='21' OR gender='female'
This will output result if one of the two condition is true,  age & gender.

Limit output to 5 result:
SELECT * FROM person LIMIT 5

Sort result by Ascending/Descending order:
SELECT * FROM person ORDER BY name ASC
SELECT * FROM person ORDER BY name DESC

Query name which contain the letter  a.
SELECT * FROM person WHERE name LIKE '%a%'

Query only unique name:
SELECT DISTINCT(name) FROM person
This query is useful when we have many duplicate data of the same value.

Count total line in the table:
SELECT COUNT(name) FROM person

19 April 2013

Basic SQL tutorial



INTRO:
SQL is a programming languages designed for managing data. It is used for data storage or database storage. MySQL is the most popular among database management storage system. Others are Oracle, Windows Access, PostgreSQL, SQLite etc. Database are use from Office productivity to storage of various data type.

This tutorial will cover all the basic of the above and more database system..

SQL look like this..
SELECT * FROM table

Create a database:
CREATE DATABASE test
This will create a database with name  'test'.

Create a table:
CREATE TABLE person ( `name` VARCHAR(12), `age` VARCHAR(3), `gender` VARCHAR(6) )
This will create a table with name  'person' having column  name, age, gender.

Add a data to table:
INSERT INTO person (name, age, gender) VALUES ('anil', '18', 'male')
This will add a line into the table  person  to the column  name, age, gender  with value  anil, 18, male  respectively.

Check if the data/line has been added:
SELECT * FROM person
Output:
name age gender
anil 18 male


Note:
* the number in  VARCHAR(12)  signify only 12 characters will be saved and rest omitted. If we enter a name  'verylonglongname' (16 char), the saved name will only be  'verylonglong' (12 char), last 4 char omitted.