SQL alias select - sql

I need to reorder my query to have the name in the first column, but I still need the rest of the columns populated as well.
How would you implement an alias in oracle to achieve this query?
select s.name, s.* from table as s
Thanks for the help.

There is no simple way around this, you must specify all columns in the order you would like them.
select s.name, s.column1, s.column2, ... from table as s

Related

In SQL how can I remove duplicates on a column without including it in the SELECT command/ the extract

I'm trying to remove duplicates on a column in SQL, without including that column in the extract (since it contains personally identifiable data). I thought I might be able to do this with nested queries (as below), however this isn't working. I also thought it might be possible to remove duplicates in the WHERE statement, but couldn't find anything from googling. Any ideas? Thanks in advance.
SELECT [ETHNIC], [RELIGION]
FROM
(SELECT DISTINCT [ID], [ETHNIC], [RELIGION]
FROM MainData)
Using distinct like that will apply distinct to the row, so if there are two rows with the same ID but different ETHNIC and RELIGION the distinct won't remove them. To do that you could use group by in your query, but then you need to use an aggregation (e.g. max):
SELECT [ETHNIC], [RELIGION]
FROM
(SELECT [ID], MAX([ETHNIC]) AS ETHNIC, MAX([RELIGION]) AS RELIGION
FROM MainData
GROUP BY [ID])
If that's not what you're looking for, some SQL dialects require that you name your inner select, so you could try adding AS X to the end of your query.

Is there a way querying alias and partition together?

I want to querying like
select enp.LAST_NAME from EMPLOYEES enp partition("P20120101");
When I try It oracle says;
> ORA-00924: missing BY keyword
Is there a way to use alias and partition together?
The correct syntax is
FROM <TABLE_NAME> PARTITION(<PARTITION_NAME>) <ALIAS_NAME>
So,
try select enp.LAST_NAME from EMPLOYEES partition("P20120101") enp;

SQL GROUP BY 1 2 3 and SQL Order of Execution

This may be a dumb question but I am really confused. So according to the SQL Query Order of Execution, the GROUP BY clause will be executed before the SELECT clause. However it allows to do something like:
SELECT field_1, SUM(field_2) FROM myTable GROUP BY 1
My confusion is that if GROUP BY clause happens before SELECT, in this scenario I provided, how does SQL know what 1 is? It works with ORDER BY clause and it makes sense to me because ORDER BY clause happens after SELECT.
Can someone help me out? Thanks in advance!
https://www.periscopedata.com/blog/sql-query-order-of-operations
My understanding is because it's ordinal notation and for the SELECT statement to pass syntax validation you have to have at least selected a column. So the 1 is stating the first column in the select statement since it knows you have a column selected.
EDIT:
I see people saying you can't use ordinal notation and they are right if you're using SQL Server. You can use it in MySQL though.
select a,b,c from emp group by 1,2,3. First it will group by column a then b and c. It works based on the column after the select statement.
Each GROUP BY expression must contain at least one column that is not an outer reference. You cannot group by 1 if it is not a column in your table.

Oracle query mistake

I need to know where the mistake is in this oracle query?
SELECT(KEY1),COUNT(*) FROM TABLE1 GROUP BY AGE
SELECT KEY1,COUNT(*) FROM TABLE1 GROUP BY KEY1
There are two problems. First one: You cannot close the parenthesis after the first keyword. Second: You have to group by all keys that are in the query that are not all row dependend. In that case "KEY1". If you want to order by age you have to query age as parameter.
SELECT AGE,COUNT(*) FROM TABLE1 GROUP BY AGE
Your table naming is not very good. I assume you should have a look at group by tutorials like https://www.w3schools.com/sql/sql_groupby.asp or the sql tutorial https://www.w3schools.com/sql/
Your query had an issue. You have to modify your query as below
SELECT KEY1,COUNT(*) FROM TABLE1 GROUP BY KEY1.
Observation:
All the columns that are added in the select statement alongside the aggregate functions, should be included the group by columns.
Your first column does have the bracket in it which should be removed.

group by not working SQL- very basic

i did a few searches and i saw a few people try to do some kind of nested select statement in order to fix the issue. i did not understand it.
can someone help me please:
the data is already sorted by provider name, each provider name is listed more than once based on various other columns in the table. however, when i do this, i do not get one line per provider name. instead the provider names repeat as if i am not using group by
here is the code:
create table moopnjsummary2 as
select mnj.ProviderName
from moopnj mnj
group by mnj.ProviderName
Do you want a list of mnj.ProviderName without repeats? What is your final goal?
You could also try SELECT DISTINCT
select mnj.ProviderName, count(*) as Providernamecount
from moopnj mnj
group by mnj.ProviderName
If all you're looking for is distinct ProviderNames, try running
SELECT DISTINCT moopnj.ProviderName FROM moopnj
This is a not a full answer to your question but if all you want is a list of provider names you should use: "Select DISTINCT mnj.ProviderName ...". That will eliminiate all duplicates.