Distinct with select * - sql

Is it possible to use select * with distinct or write easily something that has the same impact?
I need to select all columns from a table with distinct value, but listing all the columns in select clause would be nerve-breaking because the number of columns is over 20!

In Microsoft SQL Server you can write:
select distinct * from MyTable
However, it is considered "best practice" to specify the columns explicitly, partly because it improves the performance of the query, but also to protect yourself from failures that would arise if the database schema were to change in the future

This should work:
SELECT DISTINCT * FROM TABLE_NAME

Use this query:
SELECT DISTINCT Employee, Rank
FROM Employees

Adding the "distinct" keyword right after "select" does the work.
For example:
SELECT DISTINCT * FROM TABLE_NAME

Related

T-SQL SELECT statement

I have a simple question regarding SELECT statement in SQL Server. I would like to know the purpose of the following syntax:
SELECT column_name, . *
I don't understand the purpose of the (period) and a (star) after the SELECT. I understand SELECT column_name1, column_name2,.... etc. or SELECT *...
but what does a period do before the star.
That is invalid syntax and will not run.
.* can be used following a table name or alias to get all columns for that table. For example...
SELECT mytable.* FROM mytable
or
SELECT a.column_one, a.* FROM mytable a

Ordering by expression from Select

I need to make a query like this:
SELECT (t.a-t.b) AS 'difference'
FROM t
ORDER BY abs(t.a-t.b)
Is there a way not to duplicate code (t.a-t.b) ? Thank you for your answers
You can wrap the SQL statement and then perform the ORDER BY if you're performing an absolute value on it.
SELECT * FROM
(
SELECT (t.a-t.b) AS "difference"
FROM t
) a
ORDER BY abs(a.difference)
UPDATE: I used SQL Server the 1st time, but depending on your environment (Oracle, MySQL), you may need to include double quotes around the column alias, so:
SELECT * FROM
(
SELECT (t.a-t.b) AS "difference"
FROM t
) a
ORDER BY abs("a.difference")

Query works in MySQL not Oracle

The following SQL statement works in MySQL but not with Oracle:
SELECT *, MAX(COLUMN_A)
FROM table_xyz
WHERE COLUMN_A <= 100
GROUP BY COLUMN_A
Oracle complaint: "FROM keyword not found where expected"
actually the statement was incorrect, we were not grouping by COLUMN_A but another column instead. actually what we want is this
SELECT *, MAX(COLUMN_A)
FROM table_xyz
WHERE COLUMN_A <= 100
GROUP BY COLUMN_B
this works but gives us only column A and B
SELECT COLUMN_B, MAX(COLUMN_A)
FROM table_xyz
WHERE COLUMN_A <= 100
GROUP BY COLUMN_B
what we want is this, but it doesn't work (group by error)
SELECT COLUMN_B, COLUMN_C .... COLUMN_X, MAX(COLUMN_A)
FROM table_xyz
WHERE COLUMN_A <= 100
GROUP BY COLUMN_B
That's because Oracle requires you to define all the columns not wrapped in an aggregate function (MIN, MAX, COUNT, etc). SQL Server would return a similar error. MySQL's behavior is documented here.
Because your query is using SELECT *, I can't re-write it properly for you. But I also can't guarantee a syntactically correct version would return the same results as you see on MySQL either. Grouping by the same column you want the MAX is quite odd...
If you want the max() for column_a you don't need the group by at all:
SELECT MAX(COLUMN_A)
FROM table_xyz
WHERE COLUMN_A <= 100
In addition to what everyone else is saying, Oracle does not allow mixing * with explicit column definitions in queries:
SQL> select *, table_name from user_tables;
select *, table_name from user_tables
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
Oracle hasn't even looked at the fact that you are trying to get columns outside of those included in the group by clause. Which as others have stated, Oracle will not do.
This doesn't answer your MAX issue, but the only way to follow a '*' with other columns is if you use an explicit reference to a table alias - e.g.
SELECT e.*, zip_code
FROM addresses a,
employees e
WHERE e.addressId = a.Id
For the MAX value, you will either need to group by all other columns, or look into analytic functions (plenty of previous answers on Stack Overflow).
Multiple problems. Your GROUP BY clause is backwards. You need to define your GROUP BY by the columns in the *. Also what OMG Ponies said before.

Number of rows in Oracle SQL Select?

I need to know how many records were returned in a select in oracle. Currently, I do two queries:
SELECT COUNT(ITEM_ID) FROM MY_ITEMS;
SELECT * FROM MY_ITEMS;
I need to know the COUNT but I hate doing two queries. Is there a way to do:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
Is there a way to do:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
If you want it to be in this exact order, you can fetch all records on the client and count their number (almost all client libraries provide a function for that).
You can also do:
SELECT i.*, COUNT(*) OVER ()
FROM my_items i
, which will return you the count along with each record.
If you're working in PL/SQL, you can use the SQL%ROWCOUNT pseudo-variable to get the number of rows affected by the last SQL statement. Might save you some effort.
This ought to do the trick.
WITH
base AS
(
SELECT *
FROM MY_ITEMS
)
SELECT (SELECT COUNT(*) FROM base) kount,
base.*
FROM base
I'm just unsure about the table aliases, I don't remember in Oracle if they require 'AS' or not. But this should work.
select mt.*, c.Cntr
from MyTable mt
, (select COUNT(*) as Cntr
from MyTable
) c

Why shouldn’t you use DISTINCT when you could use GROUP BY?

According to tips from MySQL performance wiki:
Don't use DISTINCT when you have or could use GROUP BY.
Can somebody post example of queries where GROUP BY can be used instead of DISTINCT?
If you know that two columns from your result are always directly related then it's slower to do this:
SELECT DISTINCT CustomerId, CustomerName FROM (...)
than this:
SELECT CustomerId, CustomerName FROM (...) GROUP BY CustomerId
because in the second case it only has to compare the id, but in the first case it has to compare both fields. This is a MySQL specific trick. It won't work with other databases.
SELECT Code
FROM YourTable
GROUP BY Code
vs
SELECT DISTINCT Code
FROM YourTable
The basic rule : Put all the columns from the SELECT clause into the GROUP BY clause
so
SELECT DISTINCT a,b,c FROM D
becomes
SELECT a,b,c FROM D GROUP BY a,b,c
Example.
Relation customer(ssnum,name, zipcode, address) PK(ssnum). ssnum is social security number.
SQL:
Select DISTINCT ssnum from customer where zipcode=1234 group by name
This SQL statement returns unique records for those customer's that have zipcode 1234. At the end results are grouped by name.
Here DISTINCT is no not necessary. because you are selecting ssnum which is already unique because ssnun is primary key. two person can not have same ssnum.
In this case Select ssnum from customer where zipcode=1234 group by name will give better performance than "... DISTINCT.......".
DISTINCT is an expensive operation in a DBMS.