Results 1 to 8 of 8

Thread: SQL syntax

  1. #1
    Join Date
    Jun 2010
    Posts
    3

    Unanswered: SQL syntax

    Hi guys im extremely new to SQL but im learining.

    I would really appreciate if someone can check my sql statment and syntax just to check if it is correct.

    its only a few lines

    Assuimng "users" as the table and "age" as a column holding numeric data.

    I'm trying to view all persons 18 and over in descending order,

    SELECT age
    FROM users
    WHERE age >=18
    ORDER BY age DESC

    Regards

    jon

    thanks in advance.

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Just to state the obvious, did it work when you ran it? Because if it did then it's likely it is correct.

  3. #3
    Join Date
    Jun 2010
    Posts
    3

    Not sure if it works

    No I have not got the actual database, this is just a question asked by my school.

    So im not sure if it wil work, I just want someone to check it in theory.

    Thanks for your reply

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Do you have SQL Server Express installed?

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Assuming you do:
    Code:
    USE test
    GO
    
    IF EXISTS (SELECT NULL FROM sys.tables WHERE object_id = OBJECT_ID('dbo.users')) 
    BEGIN
        DROP TABLE dbo.users
    END
    
    CREATE TABLE dbo.users
        (
            id       INT            IDENTITY(1, 1)                  NOT NULL
          , name     VARCHAR(99)    COLLATE Latin1_General_CI_AS    NULL 
          , age      TINYINT                                        NULL
          , CONSTRAINT pk_users PRIMARY KEY CLUSTERED (id) WITH (FILLFACTOR = 100)
        ) ON [PRIMARY]
    GO
    
    INSERT INTO dbo.users
        (
            name
          , age
        )
    SELECT  name        = REPLICATE(CHAR((number % 26) + 64), ABS(LEFT(CHECKSUM(*), 2)))
          , age         = (number % 100)
    FROM    master.dbo.spt_values
    WHERE   number > 0
    
    SELECT age
    FROM users
    WHERE age >=18
    ORDER BY age DESC

  6. #6
    Join Date
    Feb 2008
    Posts
    21
    Quote Originally Posted by thepower1986 View Post
    Hi guys im extremely new to SQL but im learining.

    I would really appreciate if someone can check my sql statment and syntax just to check if it is correct.

    its only a few lines

    Assuimng "users" as the table and "age" as a column holding numeric data.

    I'm trying to view all persons 18 and over in descending order,

    SELECT age
    FROM users
    WHERE age >=18
    ORDER BY age DESC

    Regards

    jon

    thanks in advance.
    It seems ok.

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Syntactically, yes.

    But not if you consider the wording and the intention of the request. Hence the testing script.

  8. #8
    Join Date
    Jun 2010
    Posts
    3

    sql server

    OK, I actually just finished installing sql so I'll have a crack at it now.
    Thanks for the help and I will let you know how i get on.

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •