SQL Code:
1 2 3 4 5 6 7 8 | SELECT column1, column2, ... FROM table_name; SELECT * FROM Table_Name /* Select all columns from Table */ SELECT TOP 10 FROM Table_Name /* Select top 10 Result */ |
The SELECT statement is used to select data from a database. We will call the set of result as SQL result-set.
In the first selection statement of the query user can specify the columns to select from the table. This selection statement is used when we need to select the columns in particular order or want to select only some particular columns of table.
SELECT * FROM Table_Name – This will give all the columns in the table in particular order in which table designed. Normally when we query the table developers mostly use the this type of selection query.
SELECT TOP 10 FROM Table_Name – This query will give the result of 10 rows in the result-set. After top we can specify the number of rows needs to select from the table. This top select statement is mostly used to limit the number of rows selection.
SQL SELECT Examples
Example 1: List all the records in the student chart
1 2 3 | select * from students |
Example 2: List the gender Female (F) records in the student table
1 2 3 | select * from students where gender='F' |
Example 3: List the names, surnames and classes of the students in the class 10Math or 10Sci in the student table
1 2 3 4 | select name, surname, class from students where class='10Math' or class='10Sci' |
Example 4: List the book names and pages count with number of pages between 50 and 200 in the book table
1 2 3 | select name from books where pagecount between 50 and 200 |
Example 5: List the names surnames classes and genders of males in 9Math or females in 9His in the student table
1 2 3 4 | select name,surname,class,gender from students where (class='9Math' and gender='M') or (class='9His' and gender='F') |
Example 6: List the males whose classes are 10Math or 10Bio
1 2 3 4 | select name, surname, class, gender from students where (sinif='10Bio' or sinif='10Math') and cinsiyet='M' |