SQL查询出错(嵌套的select语句)

时间:2013-09-22 02:24:59

标签: sql oracle select

我不确定为什么我在以下SQL查询中收到语法错误。

查询的目标(以及稍后列出的架构/创建的视图)是查找经理的名称和公司名称,这些经理的收入超过所有经理的平均工资。< / p>

这是我写的查询及其附带的错误:

select ManagerLastname, MFirstName, MMidInitial, company_name
  from ManagerInfo, Works
    (select avg(salary) as AverageManagerSalary, Lastname
     from Works,
     group by Lastname) Work1
where ManagerInfo.ManagerLastname = Work1.Lastname and
Works.salary > Work1.AverageManagerSalary;

ORA-00933: SQL command not properly ended : select ManagerLastname, MFirstName, MMidInitial, company_name from ManagerInfo, Works (select avg(salary) as AverageManagerSalary, Lastname from Works, group by Lastname) Work1 where ManagerInfo.ManagerLastname = Work1.Lastname and Works.salary > Work1.AverageManagerSalary

我不确定此查询中语法错误的位置。我认为所有括号和“标点符号”都是正确的所以我认为这是嵌套 select 语句的问题,但我不确定在该语句中需要更改的内容......

以下是我的数据库的整体架构。这个模式以及我创建的上面视图“正确编译”,所以我知道错误与查询代码本身有关。

create table Employee(
    Lastname    varchar(10),
    FirstName   varchar(10),
    MidInitial  char(1),
    gender      char(1),
    street      varchar(10),
    city        varchar(10),
    primary key(Lastname, FirstName, MidInitial));

create table company(
    company_name    varchar(20),
    city    varchar(10),
    primary key(company_name));

create table Works(
    Lastname    varchar(10),
    FirstName   varchar(10),
    MidInitial  char(1),
    company_name    varchar(20),
    salary      numeric(8,2),
    primary key(Lastname, FirstName, MidInitial, company_name),
    foreign key(Lastname, FirstName, MidInitial) references Employee,
    foreign key(company_name) references company);

create table Manages(
    Lastname    varchar(10),
    FirstName   varchar(10),
    MidInitial  char(1),
    ManagerLastname varchar(10),
    MFirstName  varchar(10),
    MMidInitial char(1),
    start_date  date,
    primary key(Lastname, FirstName, MidInitial, ManagerLastname, MFirstName, MMidInitial),
    foreign key(Lastname, FirstName, MidInitial) references Employee);

create view ManagerInfo as
  (select distinct ManagerLastname, MFirstName, MMidInitial, street, city
   from Manages, Employee
   where Manages.ManagerLastname = Employee.Lastname and
  Manages.MFirstName = Employee.FirstName and
  Manages.MMidInitial = Employee.MidInitial);

顺便说一下,我正在使用SQL for Oracle 11g R2。

感谢您提供的任何帮助;非常感谢。

1 个答案:

答案 0 :(得分:1)

在from子句中的Works之后缺少逗号? (第一个)然后在您的子选择中的Works之后似乎有一个额外的逗号。