Results 1 to 8 of 8

Thread: [RESOLVED] SQL SELECT queries question

  1. #1

    Thread Starter
    Addicted Member MoE70's Avatar
    Join Date
    May 2006
    Posts
    185

    Resolved [RESOLVED] SQL SELECT queries question

    i need to show title, year of movies and order them by rank. ive tried:

    Select title, year FROM movies, ratings ORDER BY rank;

    it works but everything is listed 4 times. anyone know why?
    Last edited by MoE70; Mar 23rd, 2009 at 10:19 AM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SQL SELECT queries question

    Use a JOIN.

    SELECT title, year
    FROM movies
    INNER JOIN ratings on movies.movieid=ratings.movieid
    ORDER BY rank
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Addicted Member MoE70's Avatar
    Join Date
    May 2006
    Posts
    185

    Re: SQL SELECT queries question

    works great thanks

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SQL SELECT queries question

    9) Show the names of all the actors who have not worked with director ‘Paul Greengrass’.
    SELECT * FROM actors where actorid not in
    (SELECT movie2actors.actorid FROM movie2actors
    INNER JOIN movies2directors ON movie2actors.movieid=movies2directors.movieid
    INNER JOIN directors ON movies2directors.directorid=directors.directorid
    where directors.name = 'Paul Greengrass')
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SQL SELECT queries question

    10) Show the names of actors who have worked in movies with a rank in the top 10 inclusive.
    Show the movie titles worked in and rank. Sort in descending order of rank.
    SELECT TOP 10 actors.*, movies.title
    FROM actors
    INNER JOIN movies on movies.movieid=actors.movieid
    INNER JOIN ratings on movies.movieid=ratings.movieid
    ORDER BY ratings.rank DESC
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Addicted Member MoE70's Avatar
    Join Date
    May 2006
    Posts
    185

    Re: SQL SELECT queries question

    9) works great thanks

    Quote Originally Posted by Pradeep1210 View Post
    SELECT TOP 10 actors.*, movies.title
    FROM actors
    INNER JOIN movies on movies.movieid=actors.movieid
    INNER JOIN ratings on movies.movieid=ratings.movieid
    ORDER BY ratings.rank DESC

    getting syntax errors here for Q10


    mysql> SELECT TOP 4 actors.*, movies.title
    -> FROM actors
    -> INNER JOIN movies on movies.movieid=actors.movieid
    -> INNER JOIN ratings on movies.movieid=ratings.movieid
    -> ORDER BY ratings.rank DESC;

    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near '4 act
    ors.*, movies.title
    FROM actors
    INNER JOIN movies on movies.movieid=actors.' at line 1
    mysql>

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SQL SELECT queries question

    Try this:

    SELECT TOP 10 actors.actorid, actors.name, actors.sex, movies.title
    FROM actors
    INNER JOIN movie2actors ON movie2actors.actorid=actors.actorid
    INNER JOIN movies on movies.movieid=movie2actors.movieid
    INNER JOIN ratings on movies.movieid=ratings.movieid
    ORDER BY ratings.rank DESC
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    Addicted Member MoE70's Avatar
    Join Date
    May 2006
    Posts
    185

    Re: SQL SELECT queries question

    Quote Originally Posted by Pradeep1210 View Post
    Try this:

    SELECT TOP 10 actors.actorid, actors.name, actors.sex, movies.title
    FROM actors
    INNER JOIN movie2actors ON movie2actors.actorid=actors.actorid
    INNER JOIN movies on movies.movieid=movie2actors.movieid
    INNER JOIN ratings on movies.movieid=ratings.movieid
    ORDER BY ratings.rank DESC

    that does work either. do you want to try it on my DB its pretty small heres the SQL attached to construct it
    Last edited by MoE70; Mar 23rd, 2009 at 10:18 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width