SELECT without commas between attributes? - sql

I need help understanding the following queries and why they are valid/invalid:
SELECT first_name last_name, salary FROM employee VALID
SELECT first_name, last_name salary FROM employee VALID
SELECT first_name last_name salary FROM employee INVALID
For reference, the first retrieves last_name and salary and the second retrieves first_name and salary.
Also, to note, the "extra" unprinted column on the valid lines must be an actual row. Typing something like "asfsfasfs last_name, salary" will be INVALID.

in the second case, salary is considered as an alias for last_name.
So you will get last_name value, but named salary (the column header in a resultset, for example). So it's valid, but you don't retrieve salary's value.
In the third case, you have too much spaces (an alias can't have spaces if you don't put quotes around it), so it's not valid.

Related

Count how many employees with the same name exist in the database in PostgreSQL

I have a data table of employees, I want to get how many same name employees in the database. Name information is saved as first_name and last_name. I have tried.
Select count(concat(first_name,'',last_name) as empname, (concat(first_name,'',last_name) as empname from xyz.
getting error.
Your SQL is missing some parentheses (brackets), and to use an aggregation function as well as a non-aggregated column, you must include the non-aggregated column in a GROUP BY
So your SQL should look like this:
Select count(concat(first_name,' ',last_name)) as countempname,
(concat(first_name,' ',last_name)) as empname
from xyz
GROUP BY (concat(first_name,' ',last_name));
Notice also that I added a space. It is a bit odd to include an empty string in the concat. Also as a short form, if you do not want to repeat the concat in the GROUP BY, you can replace it with the column ordinal number (in this case 2) so it becomes:
Select count(concat(first_name,' ',last_name)) as countempname,
(concat(first_name,' ',last_name)) as empname
from xyz
GROUP BY 2;
If you want to count how many times each first/last name tuple appears in the table, you can use aggregation:
select first_name, last_name, count(*) cnt
from xyz
group by first_name, last_name
This gives you one row per first/last name tuple, with the total count. Note that there is not point concatenating the variables; group by can operate of column tuples.
On the other hand, maybe you want the entire employee row, with an additional column that holds the total count of other rows having the same names. If so, you can use a window count instead of aggregation:
select x.*, count(*) over(partiton by first_name, last_name) cnt
from xyz

What select is executed first in a nested subquery?

For such a subquery, what select is executed first?
SELECT name, salary, dept_id
FROM employee
WHERE salary >
( SELECT AVG(salary) FROM employee
WHERE dept_no =
( SELECT dept_no FROM employee
WHERE last_name =
( SELECT last_name FROM employee
WHERE salary > 50000))) ;
This: SELECT last_name FROM employee ?
SQL is a declarative language, not a procedural language. That is, the query does not specify the execution path, it specifies the logic for the result set. So, any of the queries could be "executed" first, depending on what the SQL optimizer decides to do.
That said, it is probably more important to understand the query logic than to understand how it is executed (at least at this stage). Your queries are all uncorrelated, so you can actually start with either the innermost or the outermost and work from there. Something like:
Get all employees whose salary
is greater than the average salary for the department
where employees with the same last name
have a salary greater than 50,000
Whether that is how the query is executed is immaterial. Something like that is what the query will return.

ORACLE SQL dealing with different tables

I will explain the problem I am stuck on. I have a table named empl02 which contains Lastname, salary, and position for all the employees. I am asked to display last,name,salary, position for all employees making more money than the highest paid member of a certain 'position', we will call this position server. I cannot just do something simple like...
SQL> select Lastname,salary,position FROM empl02
2 WHERE
3 SAL > 125000;
Rather, it must be dynamic. I feel the logic is pretty simple I'm just not sure how to translate it into SQL. I am thinking something along the lines of
"SELECT Lastname,salary,position from empl02 where salary > MAX(SALARY) of position(server)" what is a way to translate this task to SQL?
You need to retrieve the "reference" salary as a sub-query:
select lastname, salary, position
from empl02
where salary > (select max(salary)
from empl02
where position = 'manager');

Split Full Name into First Name and Last Name fields Access

I have this query to retrieve the First Name out of the Full_Name field.
SELECT Employee_Table.Full_Name, Left([Full_Name],InStr([Full_Name]," ")-1) AS First_Name
FROM Employee_Table;
It works fine,
However, I tried to change the query to get the Last Name into the Last_Name field by changing the query to this one but it did not work. Please Help
SELECT Employee_Table.Full_Name, Right([Full_Name],InStr([Full_Name]," ")+1) AS Last_Name
FROM Employee_Table;
I would like to have only one query that pulls the information and not two separate ones.
Thanks
Regards
In your second query, you are pulling from the end of the string, but the length is from the beginning. Oops. The function that you want is MID() rather than RIGHT():
SELECT Employee_Table.Full_Name, Left([Full_Name],InStr([Full_Name]," ")-1) AS First_Name,
mid([Full_Name],InStr([Full_Name]," ")+1) as Last_Name
FROM Employee_Table;

Can't figure this error out?

Code :
SELECT * FROM Grade
WHERE grade = ‘MG’ ‘1-9’
SELECT * StaffNo, Name, DOB, ReportsTo,
FROM Staff
ORDER BY DOB DESC;
I keep getting
ORA-00911: invalid character
is this a problem with the code?
Couple of problems -
1.You either need to do a UNION between the first and second SELECT statement, in which case the columns (datatypes) on both the SELECTs should match. Also, one of the columns should be DOB
SELECT StaffNo, Name, DOB, ReportsTo --you can do * here if Grade has exactly 4 columns of same datatype as in columns in the select below
FROM Grade
WHERE grade IN ('MG', '1', '9') --Check for Missing grade or grade 1 or grade 9
UNION
SELECT StaffNo, Name, DOB, ReportsTo
FROM Staff
ORDER BY DOB DESC;
Or these are two entirely different queries like-
SELECT * FROM Grade
WHERE grade IN ('MG','1', '9');
SELECT StaffNo, Name, DOB, ReportsTo --* means all columns so its either * or just the column name specifically. Both can be done, but doesn't make sense
FROM Staff
ORDER BY DOB DESC;
2.’ is a wrong character in Oracle, it should rather be '.
3.In the second SELECT the column ReportsTo ends with a ,. This will be considered illegal by Oracle.
This is the problem, for potentially two reasons:
WHERE grade = ‘MG’ ‘1-9’
Have you actually included curly quotes in your query? If so, that's probably why Oracle is complaining about an invalid character.
However, it's then unclear exactly what you're trying to match. Do you want anything starting with MG and then a character between 1 and 9? If so, you could use:
WHERE grade BETWEEN 'MG1' AND 'MG9'
If that's not what you mean, you need to explain what you're trying to do more carefully - and understand that if a human can't understand your intention, it's even less likely that a SQL parser will...