Question

I tried to search for answers online and around here unfortunately it can't solve the problem.

Here is my code:

SELECT d.driverID, 
       e.firstname, 
       e.lastname, 
       t.testid, 
       t.testType
 FROM driver d
 LEFT JOIN employee e 
   ON (e.employeeID = d.employeeID)
INNER JOIN driver_test dt 
   ON (d.driverID = dt.driverID  WHERE dt.testDate(BETWEEN TO_DATE('01-JAN-2012', 'dd-mm-yyyy') AND TO_DATE('31-JAN-2012', 'dd-mm-yyyy')))
RIGHT JOIN test t ON dt.testID = t.testID WHERE (t.testType='Alcohol');

The problem lies on Line 4. It says missing right parenthesis. I tried adding additional ones but the problem still persists.

P.S

I'm a student currently studying Oracle SQL so my knowledge is still lacking.

Was it helpful?

Solution

There are a couple of errors in your statement

  • to join on more than one condition, you have to use AND, not WHERE
  • get rid of the ( between the column name and BETWEEN

Fixed query:

SELECT d.driverID,
   e.firstname,
   e.lastname,
   t.testid,
   t.testType
FROM driver d
  LEFT JOIN employee e
    ON (e.employeeID = d.employeeID)
 INNER JOIN driver_test dt
    ON (d.driverID = dt.driverID AND
       dt.testDate BETWEEN TO_DATE('01-JAN-2012',
                                    'dd-mm-yyyy') AND
       TO_DATE('31-JAN-2012',
                'dd-mm-yyyy'))
 RIGHT JOIN test t
    ON dt.testID = t.testID
 WHERE (t.testType = 'Alcohol');

OTHER TIPS

your correct query should be

SELECT d.driverID, e.firstname, e.lastname, t.testid, t.testType
FROM driver d
LEFT JOIN employee e ON(e.employeeID = d.employeeID)
INNER JOIN driver_test dt ON(d.driverID = dt.driverID and dt.testDate(BETWEEN TO_DATE('01-JAN-2012', 'dd-mm-yyyy') AND TO_DATE('31-JAN-2012', 'dd-mm-yyyy')))
RIGHT JOIN test t ON dt.testID = t.testID 
WHERE (t.testType='Alcohol');

the Where clause is for filtering, in this case you're concatening logical operations (in the joins). So you just need to use AND.

try this one it is clearer

SELECT d.driverID, e.firstname, e.lastname, t.testid, t.testType
FROM driver d
LEFT JOIN employee e ON(e.employeeID = d.employeeID)
INNER JOIN driver_test dt ON(d.driverID = dt.driverID)
RIGHT JOIN test t ON dt.testID = t.testID
where dt.testDate(BETWEEN TO_DATE('01-JAN-2012', 'dd-mm-yyyy') AND 
TO_DATE('31-JAN-2012','dd-mm-yyyy'))) and t.testType='Alcohol';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top