SQL(SQL/Oracle) insert value from a select statement - sql

I want to insert (user_id) value from a select statement like below. Can this query will work on both sqlserver and oracle? Kindly advise.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
values (
select max(user_id) from b_user ,
david brown,
david#david.com,
chicago,
il,
usa,
60007)

No, that query won't work in either Oracle or SQL Server. You can do an insert based on a select statement, however. Note that you also have to treat strings as strings and enclose them in quotes.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
select max(user_id) user_id,
'david brown',
'david#david.com',
'chicago',
'il',
'usa',
'60007'
from b_user

If we insert bracket () around the SELECT statement, it will work. I tried for SQL Server and it's working for me. Maybe you can try the following query for the Oracle as well.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
values (
(select max(user_id) from b_user),
'david brown',
'david#david.com',
'123 davids street',
'chicago',
'il',
'usa',
60007
)

Related

Inserting data into table with select

i`m trying to do something.... I have to insert some data in to a table but..... So here is where I end up...
INSERT into HR.my_employees
(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
SELECT
3 AS ID,
'Biri' AS LAST_NAME,
'Ben' AS FIRST_NAME,
substr(FIRST_NAME,1,1)||''||substr(LAST_NAME,1,7) AS userid,
1100 AS salary
FROM dual
UNION
SELECT
4 AS ID,
'Newman' AS LAST_NAME,
'Chad' AS FIRST_NAME,
substr(FIRST_NAME,1,1)||''||substr(LAST_NAME,1,7) AS userid,
750 AS salary
FROM dual;
any suggestion...
You cannot refer to an alias in the SELECT or WHERE clauses of a sub-query where it is defined. Generate the data in a sub-query (or a sub-query factoring clause) and then refer to it in an outer query:
INSERT into HR.my_employees(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
WITH data (id, last_name, first_name, salary) AS (
SELECT 3, 'Biri', 'Ben', 1100 FROM DUAL UNION ALL
SELECT 4, 'Newman', 'Chad', 750 FROM DUAL
)
SELECT id,
last_name,
first_name,
SUBSTR(first_name,1,1) || SUBSTR(last_name,1,7),
salary
FROM data;
or:
INSERT into HR.my_employees(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
SELECT id,
last_name,
first_name,
SUBSTR(first_name,1,1) || SUBSTR(last_name,1,7),
salary
FROM (
SELECT 3 AS id, 'Biri' AS last_name, 'Ben' AS first_name, 1100 AS salary FROM DUAL
UNION ALL
SELECT 4, 'Newman', 'Chad', 750 FROM DUAL
);
fiddle
Why do you want to use select statements? - Based on the values you provide it seems like you want to insert hardcoded values. Or does the dual table contain data you want to insert into HR.my_employees?
If you want to just insert values into the table, but not from an existing one, you can use the following structure:
INSERT INTO table_name (column1, column2, column3, ..., columnXX)
VALUES (value1, value2, value3, ..., valueXX);

SQL JOINING ISSUE

I have two different tables but both table may or may not have same records. i need to join these tables and get all the different records for both the tables
for example
CREATE TABLE sql_test_a
(
ID VARCHAR2(4000 BYTE),
FIRST_NAME VARCHAR2(200 BYTE),
LAST_NAME VARCHAR2(200 BYTE)
);
CREATE TABLE sql_test_b
(
ID VARCHAR2(4000 BYTE),
FIRST_NAME VARCHAR2(200 BYTE),
LAST_NAME VARCHAR2(200 BYTE)
);
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('1', 'John', 'Snow');
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('2', 'Mike', 'Tyson');
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('3', 'Bill', 'Keaton');
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('4', 'Greg', 'Mercury');
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('5', 'Steve', 'Jobs');
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('6', 'Stsdsdve', 'Josdsbs');
INSERT INTO sql_test_b (ID, FIRST_NAME, LAST_NAME) VALUES ('1', 'John', 'Snow');
INSERT INTO sql_test_b (ID, FIRST_NAME, LAST_NAME) VALUES ('2', 'Mike', 'Tyson');
INSERT INTO sql_test_b (ID, FIRST_NAME, LAST_NAME) VALUES ('3', 'Bill', 'Keaton');
INSERT INTO sql_test_b (ID, FIRST_NAME, LAST_NAME) VALUES ('4', 'Greg', 'Mercury');
INSERT INTO sql_test_b (ID, FIRST_NAME, LAST_NAME) VALUES ('5', 'Steve', 'Jobs');
INSERT INTO sql_test_b (ID, FIRST_NAME, LAST_NAME) VALUES ('7', 'Johhny', 'Depp');
INSERT INTO sql_test_b (ID, FIRST_NAME, LAST_NAME) VALUES ('8', 'Johhnaaaay', 'Deaaap');
these are the tables and the records in the tables
and the excepted output should be
ID FIRST_NAME LAST_NAME
1 John Snow
2 Mike Tyson
3 Bill Keaton
4 Greg Mercury
5 Steve Jobs
6 Stsdsdve Josdsbs
7 Johhny Depp
8 Johhnaaaay Deaaap
i tried different join like left outer join, full outer join etc
SELECT a.ID,a.FIRST_NAME,a.LAST_NAME
FROM sql_test_a a left outer join sql_test_b b on a.ID=b.ID
and a.FIRST_NAME=b.FIRST_NAME
and a.LAST_NAME=b.LAST_NAME
this query wont give the exact output
please help
The UNION operator returns unique rows of combined queries. So, just use
SELECT * FROM sql_test_a UNION SELECT * FROM sql_test_b
To filter the result using WHERE clause you could use subquery. For example
SELECT *
FROM (SELECT * FROM sql_test_a UNION SELECT * FROM sql_test_b)
WHERE ID > 3
See also live fiddle.
If you use a union you can get the list you need (note that union all could give you duplicate rows depending on your data):
select a.id as id, a.first_name as first_name, a.last_name as last_name
from sql_test_a a
union
select b.id as id, b.first_name as first_name, b.last_name as last_name
from sql_test_b b
Using a join is discouraged in this case since it will give you a table with more than three columns, joined on (at least) one of them.
EDIT
You mention you're using oracle. To filter this, you can do several things, among which, one is wrap the query in a temporary table
with tmp as (
select a.id as id, a.first_name as first_name, a.last_name as last_name
from sql_test_a a
union
select b.id as id, b.first_name as first_name, b.last_name as last_name
from sql_test_b b
)
select tmp.id, tmp.first_name, tmp.last_name
from tmp
where
tmp.first_name like '%whatever%';
Try this Please
( SELECT * FROM sql_test_1
) UNION ALL( SELECT * FROM sql_test_b
EXCEPT
SELECT * FROM sql_test_1 )
( SELECT * FROM sql_test_a
MINUS
SELECT * FROM sql_test_b) UNION ALL( SELECT * FROM sql_test_b
MINUS
SELECT * FROM sql_test_a )

Condition before insert SQL

In DB2 SQL, I want to write something like
insert into employees
(id, name) values (1, "emp1")
where (select count(*) from employee_registry) <= 10
Can't figure out the correct syntax though.
post edit.
i wanted to insert records only if some condition is met.
You need to have a SELECT statement somewhere in order to use the WHERE clause, so something like this might work:
insert into employees (id, name)
select 1, 'emp1' from sysibm.sysdummy1
where (select count(*) from employee_registry) <= 10
sysibm.sysdummy1 is a special system table that always has only one row.
Other method, group by Nothing + having :
insert into employees (id, name)
select '1', 'emp1' from employee_registry
group by 1 having count(*)<=10

SQL insert query for multiply rows with shared values

I have a simple table Users (name, city, country) and need to add several rows where some of values are the same (city, country). Is there a better way to insert data beside:
insert into Users (name, city, country) values
("John", "Paris", "France"),
("Anna", "Paris", "France"),
("Peter", "Paris", "France"),
("Mary", "Paris", "France")
Thank you
You can use a query like the following:
insert into Users (name, city, country)
select name, city, country
from (select 'John' as name union all
select 'Anna' union all
select 'Peter' union all
select 'Mary') as t1
cross join (select 'Paris' as city, 'France' as country) as t2
You should be able to use variables. So it would look like...
set #city="Paris";
set #country="France";
insert into Users(name, city, country) values
("John", #city, #country)
Although you could simplify this somewhat by using variables in your particular RDBMS syntax, you are stuck with inserting the same values into multiple rows of the table due to a design decision that you made when defining your Users table.
The problem is that your table is not normalized, meaning that the same information is present multiple times. A proper way of fixing this would be defining a table of countries, a table of cities referencing it, and changing Users table to reference Cities.
Your solution is correct normaly but try to replace " by ' else try it:
insert into Users (name, city, country)
select *
from (
select 'John', 'Paris', 'France' union all
select 'Anna', 'Paris', 'France' union all
select 'Peter', 'Paris', 'France' union all
select 'Mary', 'Paris', 'France'
) tmp

Why is my SQL failing? (ORA-00933: SQL command not properly ended)

Why is my SQL failing? I think it s formatted correctly, but it throws an error at me.
INSERT INTO NBOT_USERS
(ID,LAST_NAME,FIRST_NAME)
VALUES
(1002, 'Smith', 'John')
WHERE 1002 NOT IN (SELECT IT_ID FROM NBOT_USERS);
Insert queries do not have where clauses, unless you're doing INSERT ... SELECT FROM, in which case there can be a where clause in the SELECT portion.
Inserting Values with a Subquery: Example
INSERT INTO bonuses
SELECT employee_id, salary*1.1
FROM employees
WHERE commission_pct > 0.25 * salary;
With your schema:
INSERT INTO NBOT_USERS (ID,LAST_NAME,FIRST_NAME)
Select 1002, 'Smith', 'John'
From dual
WHERE 1002 NOT IN (SELECT FROM NBOT_USERS);