SQL ORA-00907 missing right parenthesis? - sql

So I am brand new to sql, and I was dabbling with creating a basic table to add and query data from. I am trying to create a table, but I keep getting an "ORA-00907 missing right parenthesis" error on the first part of the script, and I am not sure why. Here is my code:
CREATE TABLE Payroll
(
Identification_Number INTEGER(10),
Full_Name VARCHAR2(20) NOT NULL,
Position VARCHAR2(20) NOT NULL,
Salary INTEGER(20) NOT NULL
);
INSERT INTO Payroll (Identification_Number, Full_Name, Position, Salary) VALUES (1476563, 'Bob Smith', 'CEO', 6000000);
INSERT INTO Payroll (Identification_Number, Full_Name, Position, Salary) VALUES (1892345, 'Brian Smith', 'President', 5000000);
INSERT INTO Payroll (Identification_Number, Full_Name, Position, Salary) VALUES (1234567, 'Ron Smith', 'Vice President', 4000000);
SELECT * FROM Payroll;
Any suggestions?

Change your script as:
CREATE TABLE Payroll
(
Identification_Number NUMBER(10),
Full_Name VARCHAR2(20) NOT NULL,
Position VARCHAR2(20) NOT NULL,
Salary NUMBER(20) NOT NULL
);

Related

Error in INSERT INTO while creating TIMESTAMP from DD-MM-YY HH.MM.SS input

UPDATE: With a more articulate example.
there are create queries mentioned in below link written in MySQL syntax, can you create them the similar way in postgres wrt timestamp values:
https://www.techbeamers.com/sql-query-questions-answers-for-practice/
You would notice all the timestamp values of WORKER table in this link are in format DD-MM-YY HH.MM.SS and not DD-MM-YY HH:MM:SS
What I've tried
While creating a table using Mysql
I could do below using DATETIME:
CREATE TABLE Worker (
WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
FIRST_NAME CHAR(25),
LAST_NAME CHAR(25),
SALARY INT(15),
JOINING_DATE DATETIME,
DEPARTMENT CHAR(25)
);
INSERT INTO Worker
(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT)
VALUES
(001, 'Monika', 'Arora', 100000, '14-02-20 09.00.00', 'HR')
This returns no error in MYSQL
However, when I use TIMESTAMP (counterpart) in Postgres:
CREATE TABLE Worker(
WORKER_ID SERIAL PRIMARY KEY,
FIRST_NAME VARCHAR(250) NOT NULL,
LAST_NAME VARCHAR(250),
SALARY INT,
JOINING_DATE TIMESTAMP,
DEPARTMENT VARCHAR(15)
);
INSERT INTO Worker
(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT)
VALUES
(001, 'Monika', 'Arora', 100000, '14-02-20 09.00.00', 'HR')
I get an error: Incorrect timestamp syntax
So for now, I am using below logic
I create JOINING_DATE VARCHAR(20), i.e. create joining date in varchar,
then alter the table:
CREATE TABLE Worker(
WORKER_ID SERIAL PRIMARY KEY,
FIRST_NAME VARCHAR(250) NOT NULL,
LAST_NAME VARCHAR(250),
SALARY INT,
JOINING_DATE VARCHAR(250),
DEPARTMENT VARCHAR(15)
);
INSERT INTO Worker
(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT)
VALUES
(001, 'Monika', 'Arora', 100000, '14-02-20 09.00.00', 'HR'),
(002, 'Niharika', 'Verma', 80000, '14-06-11 09.00.00', 'Admin'),
(003, 'Vishal', 'Singhal', 300000, '14-02-20 09.00.00', 'HR'),
(004, 'Amitabh', 'Singh', 500000, '14-02-20 09.00.00', 'Admin'),
(005, 'Vivek', 'Bhati', 500000, '14-06-11 09.00.00', 'Admin'),
(006, 'Vipul', 'Diwan', 200000, '14-06-11 09.00.00', 'Account'),
(007, 'Satish', 'Kumar', 75000, '14-01-20 09.00.00', 'Account'),
(008, 'Geetika', 'Chauhan', 90000, '14-04-11 09.00.00', 'Admin');
ALTER TABLE worker
ALTER COLUMN joining_date
TYPE TIMESTAMP WITH TIME ZONE
USING to_timestamp(joining_date, 'DD-MM-YYYY HH24:MI:SS');
Is there a better way to do this just like in SQL, can't postgres SQL read '14-02-20 09.00.00' without errors?

Get value from table to use it in insert

I suppose this question was already asked somewhere here, but I have no idea how to name (and look for) it correctly.
The database:
CREATE TABLE department (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(100) NOT NULL UNIQUE
);
CREATE TABLE employee (
id INTEGER PRIMARY KEY AUTOINCREMENT,
department_id INTEGER NOT NULL,
chief_id INTEGER,
name VARCHAR(100) NOT NULL UNIQUE,
salary INTEGER NOT NULL,
FOREIGN KEY (department_id) REFERENCES department (id),
FOREIGN KEY (chief_id) REFERENCES employee (id)
);
INSERT INTO department (name) VALUES ('sales'), ('it'), ('management');
INSERT INTO employee (department_id, chief_id, name, salary) VALUES
(3, NULL, 'Owner', 1000000),
(2, 1, 'Team manager', 9000),
(2, 3, 'Senior dev #1', 7000),
(2, 3, 'Senior dev #2', 7000);
Now in insert I should calculate chief_id on my own, but I'm curious if there is a possibility to get id by name, something like
SELECT id FROM employee WHERE name = 'Owner'
and use this value instead of hardcoded id in insert.
I've tried putting select statement instead of id but that does not work.
I am using SQLite.
You can do it with a subquery as yours, but the data has to be in the table before you can select it. Otherwise the subquery returns null. So you need to break the one INSERT into multiple INSERTs.
INSERT INTO employee (department_id, chief_id, name, salary) VALUES
(3, NULL, 'Owner', 1000000);
INSERT INTO employee (department_id, chief_id, name, salary) VALUES
(2, (SELECT id FROM employee WHERE name = 'Owner'), 'Team manager', 9000);
INSERT INTO employee (department_id, chief_id, name, salary) VALUES
(2, (SELECT id FROM employee WHERE name = 'Team manager'), 'Senior dev #1', 7000),
(2, (SELECT id FROM employee WHERE name = 'Team manager'), 'Senior dev #2', 7000);
But note, that the subquery must return only one row. So the column you're checking in it's WHERE clause must contain unique values. Usually a name is not unique, there are a lot of John Smith. Usually the ID is the unique identifier. So in a general inserting the ID directly is the right approach.

Why is my sql query returning no records?

Schema:
created table dept
CREATE table dept
(dept_id varchar (20) default 'department' NOT NULL,
locat varchar (25) default 'location' NOT NULL,
nme varchar (20) default 'name' NOT NULL);
inserting values
INSERT INTO dept(dept_id, locat, nme)
VALUES (1, 'dublin', 'payroll');
INSERT INTO dept(dept_id, locat, nme)
VALUES (2, 'galway', 'manufacturing');
INSERT INTO dept(dept_id, locat, nme)
VALUES (3, 'cork', 'sales');
created table emp
create table emp
(emp_id varchar (20) default 'id' NOT NULL,
NME varchar (20) default 'name' NOT NULL,
job_title varchar (25) default 'job' NOT NULL,
HIRE_DATE DATE,
SALARY INT (25) default '0' NOT NULL,
dept_id varchar (20) default 'dept' NOT NULL);
inserting values
INSERT INTO emp
VALUES (123, 'byrne', 'clerical', DATE ('2012-06-12'), 28000, 1);
INSERT INTO emp
VALUES (124, 'barry', 'operater', DATE ('11-07-11'), 33000,2);
INSERT INTO emp
VALUES (125, 'hynes', 'senior_operator', DATE ('26-09-13'), 36500,2);
INSERT INTO emp
VALUES (126, 'WILLIAMS', 'manager', DATE ('30-10-13'), 51000,3);
THE FOLLOWING QUERY IS RETURNING NO RECORDS
SELECT NME FROM emp
where HIRE_DATE between ('01-01-10') AND ('01-01-14');
How to change the code to get the desired output.
You proper date formats. In Oracle, you can introduce constants with the date keyword:
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (123, 'byrne', 'clerical', DATE '2012-06-12', 28000, 1);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (124, 'barry', 'operater', DATE '2011-07-11', 33000, 2);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (125, 'hynes', 'senior_operator', DATE '2013-09-26', 36500, 2);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (126, 'WILLIAMS', 'manager', DATE '2013-10-30', 51000, 3);
select NME
from emp
where HIRE_DATE between date '2010-01-01' and date '2014-01-01';
I thought 2-digit years when out in 1999. Oracle might not be interpreting two-digit years the same way that you do.
Also, include the column list when doing an insert.

SQL Help(Date Functions)

I need help on SQL Queries.
TABLE:
Create Table Employees
( employee_id number(3) Primary Key,
first_name varchar2(10),
last_name varchar2(10),
dept_code varchar2(3),
hire_date date,
credit_limit number(4,2),
phone_ext varchar2(4),
manager_id number(3)
);
Entries:
insert into Employees values (201, 'Susan', 'Brown', 'Exe', To_Date('01-Jun-1998','DD-Mon-YYYY'), 30, '3484', null);
insert into Employees values (202, 'Jim', 'Kern', 'Sal', To_Date('16-Aug-1999','DD-Mon-YYYY'), 25, '8722', 201);
insert into Employees values (203, 'Martha', 'Woods', 'Shp', To_Date('02-Feb-2004','DD-Mon-YYYY'), 25, '7591', 201);
insert into Employees values (204, 'Ellen', 'Owens', 'Sal', To_Date('01-Jul-2003','DD-Mon-YYYY'), 15, '6830', 202);
I need a query to List all the employees, their hire dates and the number of days each person will have worked for the company as of January 1, 2007.
You can use YEAR() on a date field:
SELECT * FROM Employees WHERE YEAR(hire_date)='2003'
if it doesn't work, you can use EXTRACT():
SELECT * FROM Employees WHERE EXTRACT(YEAR FROM hire_date)='2003'
and, as Alan Hadsell rightly commented:
SELECT COUNT(*) FROM Employees WHERE EXTRACT(YEAR FROM hire_date)='2003'

Oracle 00904 error

I am getting this error when trying to upload this script to Oracle. Does it have to do with my insert commands?
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
CREATE TABLE EMPLOYEE(
EMPLOYEE_ID NUMBER (15) PRIMARY KEY,
FIRST_NAME VARCHAR2(15) NOT NULL,
LAST_NAME VARCHAR2(15) NOT NULL,
);
INSERT INTO EMPLOYEE VALUES ('1001', 'JOHN', 'SMITHSON');
INSERT INTO EMPLOYEE VALUES ('1002', 'WILL', 'SMITH');
INSERT INTO EMPLOYEE VALUES ('1003', 'JASON', 'BOURNE');
INSERT INTO EMPLOYEE VALUES ('1004', 'RANDY', 'MARSH');
INSERT INTO EMPLOYEE VALUES ('1005', 'ANGELA', 'CARTMAN');
INSERT INTO EMPLOYEE VALUES ('1006', 'JANE', 'DOE');
INSERT INTO EMPLOYEE VALUES ('1007', 'MARY', 'JONES');
you have a extra , at the end of create table
LAST_NAME VARCHAR2(15) NOT NULL, -- remove ,
);
the varchar to number conversion works but better to give data in number for the first column
You shouldn't be putting apostrophes around your numbers. Try it without them.