You are here
Home > Sql Server > SQL Query (SELECT Statement)

SQL Query (SELECT Statement)

SQL Query

  • SQL stands for structured query language.
  • Query may retrieve data from database.
  • SQL Queries are select, insert, update, delete etc.

    Select Statement:

  • Select statement is used to retrieve data from one or more tables.
  • If you want to select all columns available in the table, use following syntax.
  • Syntax: select * from <table name>;
  • If you want to select particular columns available in the table, use following syntax.
  • Syntax:
  • select [column-list]

    from <table name>;

  • The Select statement has many clauses.
  • Syntax of Select statement:
  • select [column-list]

    from <table name>

    [where clause]

    [group by clause]

    [having clause]

    [order by clause];

  • Here, column-list includes one or more columns. The clauses are optional.
  • Where: Where specifies which rows to retrieve.
  • Order by: It is used to sort the data in ascending or descending order.
  • Database Table : student_name
    Id firstname lastname mname
    1 Juhi Patel J
    2 Khyati Patel A
    3 Karan Shah P
    4 Rohit Shah M
    5 Anil Patel R
    6 Smita Patel A
    7 Komal Patel A
    8 Anil Patel R
    9 Smita Patel J
    10 Khusi Joshi H

 

  • Examples:
  • 1) Retrieve all rows and columns.
  • select * from student_name;

    2) Retrieve lastname from table.

    select lastname from  student_name;

    3) Retrieve firstname  with Id 2 from table.

    select firstname from  student_name  where Id=2;

    4) Retrieve firstname from table sorted by Id  column.

    select firstname from  student_name     order by Id;

    5) Retrieve firstname, lastname from table sorted descending by Id column.

    select firstname from  student_name order by Id desc;

  • Distinct: It is used to eliminate duplicate values. It is only use with select statement.
  • Syntax: select distinct [column-list]  from <tablename>;
  • Examples:
  • 1) Remove duplicate values from above table student_name.select distinct * from student_name;

    2) For column firstname remove duplicate values from above table student_name.

    select distinct firstname from student_name;

    3) For column lastname,mname remove duplicate values from above table student_name.

    select distinct lastname, mname from student_name;

Keep visiting my site ask2tech.com for upcoming top technical article

Learn more about DBA at TechNet Consultancy

Leave a Reply

Top