Skip to main content
Engineering LibreTexts

10.2: SELECT Statement

  • Page ID
    93699
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    The SELECT statement, or command, allows the user to extract data from tables, based on specific criteria. It is processed according to the following sequence:

    SELECT DISTINCT item(s)
    FROM  table(s)
    WHERE  predicate
    GROUP BY  field(s)
    ORDER BY fields

    We can use the SELECT statement to generate an employee phone list from the Employees table as follows:

    SELECT  FirstName, LastName, phone
    FROM Employees
    ORDER BY LastName

    This action will display employee’s last name, first name, and phone number from the Employees table, seen in Table \(\PageIndex{1}\).

    Last Name First Name Phone Number
    Hagans Jim 604-232-3232
    Wong Bruce 604-244-2322
    Table \(\PageIndex{1}\): Employees table. (Copyright; author via source)

     

    In this next example, we will use a Publishers table (Table \(\PageIndex{2}\)). (You will notice that Canada is mispelled in the Publisher Country field for Example Publishing and ABC Publishing. To correct mispelling, use the UPDATE statement to standardize the country field to Canada – see UPDATE statement later in this chapter.)

    Publisher Name Publisher City Publisher Province Publisher Country
    Acme Publishing Vancouver BC Canada
    Example Publishing Edmonton AB Cnada
    ABC Publishing Toronto ON Canda
    Table \(\PageIndex{2}\): Publishers table. (Copyright; author via source)

    If you add the publisher’s name and city, you would use the SELECT statement followed by the fields name separated by a comma:

    SELECT PubName, city
    FROM Publishers

    This action will display the publisher’s name and city from the Publishers table.

    If you just want the publisher’s name under the display name city, you would use the SELECT statement with no comma separating pub_name and city:

    SELECT PubName city
    FROM Publishers

    Performing this action will display only the pub_name from the Publishers table with a “city” heading.  If you do not include the comma, SQL Server assumes you want a new column name for pub_name. 


    10.2: SELECT Statement is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?