Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax near the keyword 'LEFT'

Chasing my tail trying to figure this out, googling it just leads to far more complicated examples.

I've joined two tables, which join fine. Prof has asked us to list rows from the orders table where shipped date is NULL, or in the words of the exercise, 'orders that have not shipped yet.'

Join works fine until I add the IS NULL line and then I get the error: Msg 156, Level 15, State 1, Line 13 Incorrect syntax near the keyword 'LEFT'.

I've written this 20 different ways and spent the afternoon googling it but can't get rid of the error. I know it's going to be something simple but...

SELECT
    customers.customer_id,
    customers.name,
    customers.phone,
    orders.order_id,
    orders.order_date,
    orders.shipped_date
FROM
    orders
WHERE
    orders.shipped_date IS NULL
LEFT OUTER JOIN customers ON customers.customer_id=orders.customer_id
like image 777
HandsomeRob Avatar asked Dec 12 '12 00:12

HandsomeRob


2 Answers

There is a defined order of how to compose a query

select
from
join
where
group by
having
order by

You cannot mix that order.

like image 108
juergen d Avatar answered Oct 09 '22 16:10

juergen d


LEFT JOIN should come first before the WHERE clause.

SELECT  customers.customer_id,
        customers.name,
        customers.phone,
        orders.order_id,
        orders.order_date,
        orders.shipped_date
FROM    orders 
        LEFT OUTER JOIN customers 
            ON customers.customer_id=orders.customer_id
WHERE   orders.shipped_date IS NULL

For additional information about joins, see

  • Visual Representation of SQL Joins
like image 44
John Woo Avatar answered Oct 09 '22 15:10

John Woo