Skip to main content
Engineering LibreTexts

8.3: Write a single table query in SQL

  • Page ID
    93681
  • \( \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}}} \)

    In data science, a query is a call for a specific set, group, or combination of data. In order to query a database, we need to use a language the database can understand (Gibbs, n.d.). Tables, on the other hand, are objects within a database that include some, or all, of the data from the database. This data is organized into a grouping made up of rows and columns. The rows each represent a unique item from the database records. The columns each represent different attributes that the item contains (Cai et al., n.d.). 

    StudentName 

    StudentID 

    ClassID 

    Grade 

    John 

    123 

    987 

    90 

    Jeff 

    456 

    654 

    83 

    Jason 

    789 

    321 

    97 

    Figure \(\PageIndex{1}\): Student (Kashefi 2020) 

    Above is an example table named Student. The first column represents the student’s name. This student name is what we will use to name the unique items in the rows. For example, John is the name of a student. Every other column of that row contains data that describes him. No other row will have the exact same data. This makes his, and every other row unique. 

    In the example table, the columns represent attributes in the data. Every student has a name, a student ID, a class ID, and a grade. 

    In order to query from this table, we must use specific SQL commands. We must use the SELECT command to choose the columns in the table we are querying from. To select the correct table, the FROM command must be used. In order to ask for the entire table as a query, therefore: 

    SELECT * FROM Student 

    The asterisk is a shorthand way to ask for every column. This shorthand is also used in other database and programming languages. This command would be read in plain English as “select all columns from the student table.” This would return everything from the above table, as the table is named Student and all of the information is in one column or another. 

    Now what if we just wanted to know the name and grade for each student? This is how it would look: 

    SELECT StudentName, Grade FROM Student 

    In this example, we used SELECT to choose columns. Instead of an asterisk used for “all columns,” we use the specific column names “StudentName” and “Grade,” which are intuitively the name and grade of the student, and the columns are separated by a comma. The FROM command underneath once again chooses the correct table. In plain English, this is “select the columns student name and grade from the student table.” This would be the returned table: 

    StudentName 

    Grade 

    John 

    90 

    Jeff 

    83 

    Jason 

    97 

    Figure \(\PageIndex{2}\): Student (Kashefi 2020) 

    This query can be done with any combination of columns in the table, as long as the same formatting is used. 

    The last staple command of SQL is WHERE. This command will only return a row if the information in one of the specific columns fits a specific condition of the query(“Learn SQL: 

    Queries Reference Guide,” n.d.). To allow a better understanding, here is an example: 

    SELECT StudentName, Grade 
    FROM Student  
    WHERE Grade < 95 

    For this example, we have the same queried table as above, except that any row with a grade above 95 is removed. In plain English, this would be “select the columns student name and grade from the student table if the student’s grade is below 95.” This would be the final queried table: 

    StudentName 

    Grade 

    John 

    90 

    Jeff 

    83 

    Figure \(\PageIndex{1}\): Student (Kashefi 2020) 

    This is just a small part of all the possible ways to query in SQL, but they are the main ones that can allow basic table queries to be practiced. 

     

     

     

     


    8.3: Write a single table query in SQL is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?