Order by not working in SQL analytical functions - sql

create table Employee
(
Ename varchar (255),
empno integer primary key,
deptno integer ,
salary integer
);
`
insert into Employee values ('Gauhar khan',1000,10,5000);
insert into Employee values ('Gauri sharma',1001,10,6000);
insert into Employee values ('Gauresh verma',1002,10,7000);
insert into Employee values ('Raja ashokan',1003,20,8000);
insert into Employee values ('Divya Nair',1004,20,19000);
insert into Employee values ('Ashwin Thakare',1005,20,10000);
insert into Employee values ('Uttam Thapa',1006,30,7800);
insert into Employee values ('Krutagnya Thakker',1007,30,11000);
insert into Employee values ('Kritika manocha',1008,30,12000);
`
select * from Employee;
`
SELECT empno,
deptno,
salary,
SUM(salary) OVER(PARTITION BY deptno ORDER BY salary) AS dept_running_total
FROM Employee;
It returns the error :
Error: near line 1: near "(": syntax error
I have tried using only partition by clause as well still returned the same error. Please guide me in resolving the error. I tried running this over various online sql editors.

This works fine in SQL Server 2014. I can only assume this is not actually the query you are running
SqlFiddle

Related

How to resolve this problem " near "INSERT": syntax error" in SQLite?

I cannot find what wrong in my code, but it is showing "sqlite near "INSERT": syntax error".
my code is
/*CREATE TABLE Employee
(
Emp_ID varchar not NULL PRIMARY key, --0/""
Emp_Name Varchar,
Location varchar,
commission INT,
phone_no varchar,
country varchar
);
*/
INSERT into Employee VALUES ("1","rachit","MP","1700","1234567898","india")
INSERT into Employee VALUES ("2","Swapnil","UP","1524","3485030230","india")
INSERT into Employee VALUES ("3","Hardik","MH","1469","5684586954","india")
INSERT into Employee VALUES ("4","Sarthi","GOA","1985","4995406905","india")
INSERT into Employee VALUES ("5","Rupak","KT","1654","9040938640","india")
INSERT into Employee VALUES ("6","soham","UK","1598","9894945932","india")
INSERT into Employee VALUES ("7","palash","NCR","1789","8984647458","india")
You missed to add ; at the end of each insert statement.
The correct syntax would be
INSERT into Employee VALUES ("1","rachit","MP","1700","1234567898","india");
INSERT into Employee VALUES ("2","Swapnil","UP","1524","3485030230","india");

Syntax error with the execution of SQL query, I am unable to find, kindly let me know whats the error

I am getting syntax error as syntax error near : "(", when I execute the below code. I am unable to find out what's the error.
My query:
Select id, name, AVG(salary) over (order by salary) as aver from test;
example with testdata and your statement.
it's working without syntax errors:
CREATE TABLE test
( id int
,name varchar(20)
,salary int
)
;
INSERT INTO test VALUES (1,'John', 2500);
INSERT INTO test VALUES (2,'Jack', 3500);
INSERT INTO test VALUES (3,'William', 4500);
INSERT INTO test VALUES (4,'Avarel', 7200);
Select id
,name
,AVG(salary)
over (order by salary) as aver
from test;
You need to modify your query this way
Select id, name, AVG(salary) as over from test order by salary;
Maybe it'll be helpful.

Self Join in SQL

I have an Employee table with few records in it, from which I want to get all the employees who work in the department where employee "scott" works and those with department number as 20.
can you give a try on the following query and let me know if this worked for you
select * from employee where deptno in (select deptno from employee where name ='scott')
Not very Descriptive, But u may try this as per the Information u've provided,
Create Table #Employees
(Id int,
EmpName nvarchar(20),
Deptno int
)
Insert into #Employees Values(1,'Henry',10)
Insert into #Employees Values(2,'Mark',20)
Insert into #Employees Values(3,'Scott',20)
Insert into #Employees Values(4,'David',10)
Insert into #Employees Values(5,'Peter',30)
Insert into #Employees Values(6,'Mary',20)
Insert into #Employees Values(7,'John',30)
Select EmpName From #Employees
Where Deptno In(Select Deptno from #Employees Where EmpName = 'Scott')
And for Complete information of Employees,
Select * From #Employees
Where Deptno In(Select Deptno from #Employees Where EmpName = 'Scott')
For example the following query returns employee names and their manager names for whom they are working. Copy & Paste the following sql, I think it may help the purpose.
Create table Emp
(
empid int primary key,
name varchar(50),
mgrid int
)
Insert into Emp(empid,name,mgrid)
values (1001,'Manish Agrahari',1001);
Insert into Emp(empid,name,mgrid)values (1002,'Deepti',1001);
Insert into Emp(empid,name,mgrid)values (1003,'Amit',1001);
Insert into Emp(empid,name,mgrid)values (1004,'Sandy',1002);
Insert into Emp(empid,name,mgrid)values (1005,'Ankit',1003);
Insert into Emp(empid,name,mgrid)values (1006,'Kapil',1002);
Run The following SQL and see the result:
SELECT e.empid, e.name, m.name "Manager" FROM Emp e, Emp m WHERE e.mgrid=m.empid;

How to solve single-row subquery returns more than one row

I have an employee table and it contains salary table. I want to give %10 increase to all current employees. I tried to update all employees' salary dates to specific date but I encountered problem with single-row subquery.
My database like this:
CREATE TYPE TEMPORAL_VARCHAR AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART VARCHAR2(50) );
CREATE TYPE TEMPORAL_NUMBER AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART NUMBER );
CREATE TYPE NAME_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE ADDRESS_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE DEPARTMENT_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE MANAGER_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE SALARY_TYPE AS TABLE OF TEMPORAL_NUMBER;
CREATE TABLE EMPLOYEE (
SSN NUMBER primary key,
NAME NAME_TYPE,
ADDRESS ADDRESS_TYPE ,
BIRTH_DATE DATE,
MANAGER MANAGER_TYPE ,
DEPARTMENT DEPARTMENT_TYPE,
SALARY SALARY_TYPE
)
NESTED TABLE NAME STORE AS NAME_TABLE,
NESTED TABLE ADDRESS STORE AS ADDRESS_TABLE,
NESTED TABLE MANAGER STORE AS MANAGER_TABLE,
NESTED TABLE DEPARTMENT STORE AS DEPARTMENT_TABLE,
NESTED TABLE SALARY STORE AS SALARY_TABLE
;
How to solve this problem? I tried to do this
UPDATE TABLE(
SELECT E.SALARY
FROM EMPLOYEE E
) SAL
SET SAL.VALID_TIME_UPPER_BOUND = '11.16.2015'
WHERE SAL.VALID_TIME_UPPER_BOUND = TO_DATE('12.31.9999','MM.DD.YYYY');
1st it can be duplicate
2nd see sample here
3rd in your code you need bring the where condition into select
UPDATE TABLE(
SELECT E.SALARY
FROM EMPLOYEE E
WHERE ssn in (SELECT ssn FROM EMPLOYEE e
WHERE to_date('01.01.2015','mm.dd.yyyy') in (
SELECT VALID_TIME_UPPER_BOUND FROM TABLE(e.salary)
)
)
) SAL
SET SAL.VALID_TIME_UPPER_BOUND = to_date('01.01.9999','mm.dd.yyyy')
test data
INSERT INTO EMPLOYEE(SSN, salary) values (1, SALARY_TYPE ());
INSERT INTO EMPLOYEE(SSN, salary) values (2, SALARY_TYPE ());
INSERT INTO EMPLOYEE(SSN, salary) values (3, SALARY_TYPE ());
INSERT INTO TABLE(SELECT salary FROM EMPLOYEE
WHERE ssn = 1)
VALUES (to_date('01.01.2005','mm.dd.yyyy'), to_date('01.01.2015','mm.dd.yyyy'), 1);
INSERT INTO TABLE(SELECT salary FROM EMPLOYEE
WHERE ssn = 2)
VALUES (to_date('02.02.2005','mm.dd.yyyy'), to_date('02.02.2015','mm.dd.yyyy'), 2);
INSERT INTO TABLE(SELECT salary FROM EMPLOYEE
WHERE ssn = 3)
VALUES (to_date('03.03.2005','mm.dd.yyyy'), to_date('03.03.2015','mm.dd.yyyy'), 3);
p.s do you really need the complexity?
I solved my problem using iteration like this
BEGIN
FOR employees IN (SELECT SSN FROM EMPLOYEE)
LOOP
UPDATE TABLE(
SELECT E.SALARY
FROM EMPLOYEE E
WHERE E.SSN = employees.SSN
) SAL
SET SAL.VALID_TIME_UPPER_BOUND = '11.16.2015'
WHERE SAL.VALID_TIME_UPPER_BOUND = TO_DATE('12.31.9999','MM.DD.YYYY');
END LOOP;
END;

Trigger issue - Oracle SQL plus

I have created a trigger to insert values into Payroll once an entry to Employees have been made. But when I insert values into Employees , no values are entered into Payroll from the trigger. Payroll remains empty when I insert values into Employees. Any ideas?
CREATE TABLE Employees(
empid FLOAT,
dept FLOAT,
empname varchar2(25),
salary FLOAT
);
CREATE TABLE Payroll(
empid FLOAT,
salary FLOAT
);
CREATE OR REPLACE TRIGGER NewEmployee
AFTER INSERT
ON Employees
FOR EACH ROW
BEGIN
INSERT INTO Payroll
VALUES(:Old.empid,:Old.salary);
END;
/
INSERT INTO Employees values (1,1,‘supply’,50000);
INSERT INTO Employees values (2,2,‘hard’,80000);
Select * from Employees;
Select * from payroll;
Your :Old.empid and :Old.salary should be :new.empid and :new.salary.
Also, it is slightly more efficient to use a BEFORE INSERT trigger when you can.