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
Related
Why is the use of asterisk perfectly valid in oracle sql when the asterisk is by itself in the SELECT clause, but it results in an error when there are other expressions in the SELECT?
For example:
select * from table1 -- is ok
But:
select field, * from table -- is not ok
Oracle only allows a "bare" asterisk when there are no other columns.
Otherwise, you need to qualify it:
select t.field, t.*
from table1 t;
I suspect the reason is that Oracle considers select * to be a full clause, rather than * being an abbreviation for all columns.
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")
as sample code below....
WITH sampleA as (SELECT * FROM emp)
SELECT * FROM sampleA
SELECT * FROM sampleA
this alias 'sampleA' will work for only first query not for second or later..
But I want to query more with this alias only.
Can you please help me, how can I do that?
Common table Expression scope is limited to first SELECT statement. For multiple usage , use instead temporay table or table variable.
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
I'm trying to do a subselect in pgsql aka postgresql and the example I found doesn't work:
SELECT id FROM (SELECT * FROM table);
I just needed to add an AS for the subselect, like so:
SELECT id FROM (SELECT * FROM table) AS aliasname;
I think you need something like:
SELECT * FROM table WHERE id IN (SELECT id FROM table2);
I don't understand what your non-working subquery is attempting to do, it seems like you could just say SELECT id FROM table because presently its not valid SQL92 syntax.