distinct queries

gudii9
gudii9 used Ask the Experts™
on
SELECT DISTINCT (First_Name ), distinct (last_name) FROM  CHECKED_OUT WHERE  Book_ID  not null


why above query not working
can i put one distinct for both first name and last name
are these queries case sensitive like unix?

please advise on any good links and resources, videos around this to master
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018

Commented:
Need to fix few syntax issues..

1. DISTINCT keyword is for the entire SET and it has to be specified only once for all columns in SELECT clause unless you have any Aggregate functions available.
2. In the WHERE clause it should be IS NOT NULL instead of just NOT NULL
corrected code below..
SELECT DISTINCT First_Name, last_name FROM CHECKED_OUT WHERE Book_ID IS not null

Open in new window

Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018

Commented:
Wondering whether this is any assignment or something, kindly confirm..
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
@Raja Jegan R
gudii9 appears to be undertaking a crash course in SQL, there was a flurry of similar questions earlier also asking for learning guides/places to learn/places to trial things
 =========

SELECT DISTINCT (First_Name ), distinct (last_name) FROM  CHECKED_OUT WHERE  Book_ID  not null

why above query not working because  you need   IS not null (you were missing the IS )

can i put one distinct for both first name and last name NO
In fact "select distinct" makes THE WHOLE ROW "distinct"
i.e. it compares each row to all other rows and only if a row has some difference will it remain in the result

SELECT DISTINCT First_Name, last_name FROM  CHECKED_OUT WHERE  Book_ID IS NOT NULL

PLEASE remember that the word distinct belongs to the select here, as in SELECT DISTINCT
and that, distinct IS NOT a function so it does NOT take the column name/s as a parameter

are these queries case sensitive like unix?
NO for SQL, but literals might be case sensitive

e.g.
select distinct = SELECT DISTINCT = SeLecT dIStinCT = Select Distinct

but

where column1 = 'ThisMightBeCaseSensitive'  


So, to answer this simply really isn't possible. Again "which database"  absolutely matters here.  e.g.
Oracle data is usually case sensitive, but not always
SQL Server data is usually case insensitive, but not always

So. SQL commands are NOT case sensitive but DATA can be
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Commented:
2 tips

1. Choose A database to learn SQL with.
Stick to that database until you believe you can try something else.
This will allow you to buy books suited to the database you are learning

It will also help us to help you by providing the syntax for that database.
(if you list the correct topic with your question.


2. Depending on which database you choose
http://poorsql.com/ this site will help you format your queries,  and it might detect errors for you, but this is not always true. This site is "generic" and it doesn't matter too much which database is used.

https://sql-format.com/ this site is specifically for SQL Server SQL (called "T-SQL") and it is much more accurate in finding errors. But this is much less relevant for any other databases.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial