SQL SELECT Query

What is SELECT query?

Definition: To retrieve record or records from database table, SELECT query is used.

When we want to fetch records and display, we can use SELECT query. We can use it in different way to display records from database tables.

  • By using “SELECT”

  • By using “SELECT WHERE”

  • By using “SELECT IN”

Why we needed it?

It is necessary to look what is stored in database in any system, for that we can use SELECT query. It helps us to see the data or information from database tables easily in different way.

Let’s understand all three options with syntax and example for SELECT query.

  • By using “SELECT”

    “SELECT” sql statement or query is used to retrieve data from database tables.

    Syntax:

    SELECT Column 1, Column 2, Column 3, ...
    FROM TABLENAME;


    Here, Column1, Column2, Column3 are actual columns or fields in table which we
    defined to display in actual result. Whatever columns you define here, they will get displayed only. If you want to retrieve all fields from table, you may use following syntax.


    SELECT * FROM TABLENAME; (Return all records in result)


    Example 1:

    SELECT Id, FirstName, LastName, Gender, BirthDate FROM Employee;

    In above sql select statement, only those columns will be displayed which are defined in query.


    Example 2:

    SELECT * FROM Employee;

    In above sql select statement, all the columns will be displayed from database table.

  • By using “SELECT WHERE”

    In this option, we must have to use “WHERE” clause with condition for specified column. This will return result if condition will be true only, otherwise it won’t display any records.

    Syntax:

    SELECT Column 1, Column 2, Column 3, ...
    FROM TABLENAME;
    WHERE CONDITION;


    Here, Column1, Column2, Column3 are actual columns or fields in table which we defined to display in actual result with condition in WHERE clause. If condition will be true, records will get displayed.


    Example:

    SELECT *
    FROM Employee
    WHERE Id=1;


    Results:

    Id Employee Code First Name Last name Gender Birth Date
    1 EMP_1001 John Peter Male 10-11-1982

  • By using “SELECT IN”

    In this option, we must have to use “WHERE” clause with “IN” condition to get result. This will return result if condition will be true only, otherwise it won’t display any records.


    Syntax:

    SELECT Column 1, Column 2, Column 3, ...
    FROM TABLENAME;
    WHERE CONDITION;


    Here, Column1, Column2, Column3 are actual columns or fields in table which we defined to display in actual result with condition in WHERE clause. If condition will be true, records will get displayed.


    Example:

    SELECT * FROM Employee WHERE Id IN (1,2);


    Results:

    Id Employee Code First Name Last name Gender Birth Date
    1 EMP_1001 John Peter Male 10-11-1982
    2 EMP_1002 Josh Wills Male 09-07-1980

    In above sql select statement, it display only two record set because we have defined condition Id IN (1,2) so in result set, database will display all records which has Id=1 and Id=2 value.