Better way SQL insert query - sql

Actually, I don't know what is different the following query?
Which one is better(performance, etc...)? Btw, I use SQL Server.
Query 1 :
INSERT INTO PERSON (ID, NAME, ADDRESS) VALUES('001', 'Smit', 'London');
INSERT INTO PERSON (ID, NAME, ADDRESS) VALUES('002', 'Jhon', 'London');
Query 2 : I never saw before
INSERT INTO PERSON (ID, NAME, ADDRESS)
SELECT '001', 'Smit', 'London' UNION ALL
SELECT '002', 'Jhon', 'London'

How about the multi-row syntax with table value constructors:
INSERT INTO PERSON (ID, NAME, ADDRESS)
VALUES ('001', 'Smit', 'London'), ('002', 'Jhon', 'London');

Related

Oracle simple list results to comma separated list with quotes?

I would like to get results of this simple query
select PersonID from Persons where city ='Miami'
as a flat comma separated list with quotes.
desired results should be '3','5','7','8'
http://sqlfiddle.com/#!4/95e86d/1/0
I tried list_add but im getting: ORA-00904: "STRING_AGG": invalid identifier
CREATE TABLE Persons (
PersonID NUMBER,
FirstName varchar(255),
City varchar(255)
);
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (1, 'Tom B.','Stavanger');
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (2, 'Jerry M.','Train City');
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (3, 'Eric g.','Miami');
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (4, 'Bar Y.','Manhattan');
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (5, 'John K.','Miami');
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (6, 'Foo F.','Washington');
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (7, 'Alen D.','Miami');
INSERT INTO Persons (PersonID, FirstName, City)
VALUES (8, 'John K.','Miami');
select listagg( '''' || PersonID || '''', ',' )
within group (order by personID)
from Persons
where city ='Miami'
should work.
I'd have to question why you're trying to produce this particular result. If you're going to generate this string so that you can subsequently use it in the IN list of another dynamically generated SQL statement, you're probably better off taking a step back because there are much better ways to solve the problem.
SQL Fiddle example

Queries without Aggregate or SubQuery

I'm wondering if it is possible to find all instructors that teach 'Math' and does not teach 'English', without using aggregate or subqueries.
My normal approach is to use subqueries/aggregates by finding all those that teach English and use: where instructor not in (select instructor from course where course = 'English') or to group by instructor, course having count(*) > 1.
// Test Input and Output below
CREATE TABLE testTable (instructor TEXT, course TEXT);
INSERT INTO testTable values ('John Doe', 'Math');
INSERT INTO testTable values ('John Doe', 'English');
INSERT INTO testTable values ('John Doe', 'Physics');
INSERT INTO testTable values ('Jane Doe', 'Math');
INSERT INTO testTable values ('John Smith', 'Physics');
INSERT INTO testTable values ('John Smith', 'Math');
INSERT INTO testTable values ('Janice Smith', 'English');
Solution should be:
Jane Doe
John Smith
You can do this with joins
select tm.instructor
from t tm left join
t te
on tm.instructor = te.instructor and te.subject = 'English'
where tm.subject = 'Math' and te.instructor is null;

PostgreSQL Inserting 2 relationships at once

I have 3 tables professionals, services, and feature_professional I need to insert data into when inserting a professional. This is the sql I've tried.
WITH professionals as (INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *)
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut'),
INSERT INTO feature_professional(id, professional_id, feature_id, enabled)
VALUES
(1, (select professionals.id from professionals), 1, true)
I've also tried different variations of this wrapping all the insert statements inside parenthesis but still no luck. The error I'm getting is a syntax error.
ERROR: syntax error at or near "INSERT"
LINE 106: INSERT INTO feature_professional
What is the correct syntax to accomplish this? I know that if I take out the third insert statement the query works. For example:
WITH professionals as (INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *)
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut')
You cant do two insert after a cte, so wrap each one in a different cte
SQL DEMO
WITH professionals as (
INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *
),
services as (
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut')
)
INSERT INTO features(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'autobook');
select * from professionals;
select * from services;
select * from features;
INSERT INTO feature_professional (id, professional_id, feature_id, enabled)
select ?, professionals.id from professionals, ?, ?
Provided that the question marks are values you can insert. If they aren't required you can just omit them.
INSERT INTO feature_professional (professional_id)
select professionals.id from professionals
Here is how to insert from a select instead of static values:
https://www.w3schools.com/sql/sql_insert_into_select.asp

Joining two Hierarchical queries to form larger Hierarchy

I have researched this and know I'm not the first to ask but I can't seem to get my head around it. I have created a simple example that I think will help me crack it if someone can provide the missing link!
I have a table of areas that contains continents and countries in a hierarchy.
I also have a table of places that contains cities and landmarks in a hierarchy. This table contains an area id column to join to the areas table.
create table areas
(
id NUMBER not null,
name VARCHAR2(200) not null,
parent_id NUMBER
);
-- Top Level
Insert into areas (id, name)
Values (1, 'Europe');
Insert into areas (id, name)
Values (2, 'Americas');
Insert into areas (id, name)
Values (3, 'Asia ex Japan');
Insert into areas (id, name)
Values (4, 'Japan');
-- Jurisdictions
Insert into areas (id, name, parent_id)
Values (5, 'UK', 1);
Insert into areas (id, name, parent_id)
Values (7, 'France', 1);
Insert into areas (id, name, parent_id)
Values (6, 'Germany', 1);
Insert into areas (id, name, parent_id)
Values (8, 'Italy', 1);
Insert into areas (id, name, parent_id)
Values (9, 'US', 2);
Insert into areas (id, name, parent_id)
Values (10, 'Australia', 3);
Insert into areas (id, name, parent_id)
Values (11, 'New Zealand', 3);
create table places
(
id NUMBER not null,
name VARCHAR2(200) not null,
area_id NUMBER,
parent_id NUMBER
);
Insert into places (id, name, area_id, parent_id)
Values (1, 'London', 5, NULL);
Insert into places (id, name, area_id, parent_id)
Values (2, 'Bath', 5, NULL);
Insert into places (id, name, area_id, parent_id)
Values (3, 'Liverpool', 5, NULL);
Insert into places (id, name, area_id, parent_id)
Values (4, 'Paris', 7, NULL);
Insert into places (id, name, area_id, parent_id)
Values (5, 'New York', 9, NULL);
Insert into places (id, name, area_id, parent_id)
Values (6, 'Chicago', 9, NULL);
Insert into places (id, name, area_id, parent_id)
Values (7, 'Kings Cross', 5, 1);
Insert into places (id, name, area_id, parent_id)
Values (8, 'Tower of London', 5, 1);
I can query these tables independently like this:
SELECT a.*, level FROM areas a
start with parent_id is null
connect by prior id = parent_id
SELECT p.*, level FROM places p
start with parent_id is null
connect by prior id = parent_id
Is someone able to show me the last step to join these into one query with four levels? I've been working with Oracle for years but somehow this never came up!
If there was no connect by prior in the places table, just a list of cities with an area id, would this be easier?
Thank you
Is it what you need?
with src as (
select 'A' type, a.id, a.name, a.parent_id, null area_id from areas a
union all
select 'P', -p.id id, p.name, -p.parent_id parent_id, area_id from places p)
select
src.*, level
from
src
start with
type = 'A' and parent_id is null
connect by
parent_id = prior id or
parent_id is null and area_id = prior id

What type of SQL does Red Query Builder generate?

I have been trying to use Red Query Builder visual query builder on a project, but we are finding that the SQL output is not consistent with the SQL our database (SQLite) expects. That makes me wonder what dialect of SQL is being generated and how can I change it?
Edit:
For example, this is a generated line of SQL.
"SELECT \"x0\".\"ID\", \"x0\".\"NAME\", \"x0\".\"COUNTRYCODE\", \"x0\".\"DISTRICT\", \"x0\".\"POPULATION\" FROM \"CITY\" \"x0\" INNER JOIN \"COUNTRY\" \"x1\" ON \"x0\".\"COUNTRYCODE\" = \"x1\".\"CODE\" WHERE (\"x1\".\"NAME\" = ?)"
From a cursory inspection, the x0 seems to be the alias for the table named CITY, but where is that connection being made?
The query runs just fine for me in SQLite after creating the proper tables. Here's the fully query unescaped and formatted:
SELECT
"x0"."ID",
"x0"."NAME",
"x0"."COUNTRYCODE",
"x0"."DISTRICT",
"x0"."POPULATION"
FROM
"CITY" "x0"
INNER JOIN
"COUNTRY" "x1"
ON
"x0"."COUNTRYCODE" = "x1"."CODE"
WHERE ("x1"."NAME" = ?)
Here's DDL and inserts to test:
CREATE TABLE Country (CODE text, Name Text);
CREATE TABLE CITY (ID int, Name text, countrycode text, district text, population int);
INSERT INTO Country VALUES ('US', 'United States');
INSERT INTO Country VALUES ('AU', 'Australia');
INSERT INTO City VALUES (1, 'Albany', 'US', 'NY', 1000000);
INSERT INTO City VALUES (2, 'Atlanta', 'US', 'GA', 2000000);
INSERT INTO City VALUES (3, 'Washington', 'US', 'DC', 500000);
INSERT INTO City VALUES (4, 'Melborne', 'AU', 'A', 40000);
INSERT INTO City VALUES (5, 'Sydney', 'AU', 'B', 60000);
Full example (run in console, so query is not parameterized, but ? is accepted parameterization for SQLite).
C:\Windows\System32>sqlite3
SQLite version 3.7.6.3
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE Country (CODE text, Name Text);
sqlite> CREATE TABLE CITY (ID int, Name text, countrycode text, district text, population int);
sqlite> INSERT INTO Country VALUES ('US', 'United States');
sqlite> INSERT INTO Country VALUES ('AU', 'Australia');
sqlite> INSERT INTO City VALUES (1, 'Albany', 'US', 'NY', 1000000);
sqlite> INSERT INTO City VALUES (2, 'Atlanta', 'US', 'GA', 2000000);
sqlite> INSERT INTO City VALUES (3, 'Washington', 'US', 'DC', 500000);
sqlite> INSERT INTO City VALUES (4, 'Melborne', 'AU', 'A', 40000);
sqlite> INSERT INTO City VALUES (5, 'Sydney', 'AU', 'B', 60000);
sqlite> SELECT
...> "x0"."ID",
...> "x0"."NAME",
...> "x0"."COUNTRYCODE",
...> "x0"."DISTRICT",
...> "x0"."POPULATION"
...> FROM
...> "CITY" "x0"
...> INNER JOIN
...> "COUNTRY" "x1"
...> ON
...> "x0"."COUNTRYCODE" = "x1"."CODE"
...> WHERE ("x1"."NAME" = 'United States');
1|Albany|US|NY|1000000
2|Atlanta|US|GA|2000000
3|Washington|US|DC|500000
sqlite>