selimppc

SQL Query

Apr 19th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: sr
  5.  * Date: 17/04/17
  6.  * Time: 16:01
  7.  */
  8. /*
  9.  Given the two tables below, write a SQL query that uses a join statement to list all users from the user table, their average correct answers, as well as the time of their most recently taken test. All user entries should appear, even if they have not taken a test.
  10.  
  11.  */
  12.  
  13. $sql = "
  14.  
  15.  SELECT user.user_id, CONCAT(user.first_name, ' ', user.last_name) as full_name, test_result.test_id, AVG(test_result.correct) as avg_correct, MAX(test_result.time_taken) as recent_time
  16.  
  17.  FROM user
  18.  
  19.  LEFT JOIN test_result ON test_result.user_id = user.user_id
  20.  
  21.  GROUP BY user.user_id
  22.  
  23.  ORDER BY recent_time DESC;
  24.  -- ORDER BY `avg_correct` DESC, recent_time DESC;
  25. ";
Add Comment
Please, Sign In to add comment