Search code examples
sqlnested-statement

Simple Nested SQL statement


I'm just starting to learn SQL and I have a question that may seem simple. Given the following data:

step 1 - project 1
step 2 - project 1
step 3 - project 1
step 1 - project 2
step 2 - project 2
step 1 - project 3
step 1 - project 4
step 2 - project 4
step 3 - project 4
step 4 - project 4

I'm trying to find the last step for each project. I think I should be using a nest SQL statement but I'm not sure of the proper way to do it.


Solution

  • Your query would look something like this:

    SELECT project, max(step) as step
    FROM Table_Name
    GROUP BY project
    

    The GROUP BY groups all projects together and the max() returns the max value. However, if your values are strings, you may have to remove the text and convert them to integers to properly sort and retrieve the proper max() value.