SQL - SELECT statement precedence (Query vs. Sub-Query) - sql

Hi everyone I am wondering if someone can help me understand why the Sub-Query's SELECT statement ended up producing the column name 'Code' vs. the initial SELECT statement call 'local_name' in the below screenshot. Thank you

Related

Data extraction from table using SQL query

I am very new to SQL and had a query with which I think you might be able to help me
I am trying to make a list of studies whose official_title (in the studies table) includes the keyword 'fibrillation'
I have been trying to write a code in SQL to get such information but have failed to do so, I used the following code -
SELECT * FROM studies
WHERE official_title ILIKE 'fibrillation'
I would appreciate it if you could spare a few minutes to help me out!
Thank you very much!
you can use SQL "like" to check if it contains the string you're looking for
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
For more information check this
https://www.w3schools.com/sql/sql_like.asp
SELECT * FROM studies
WHERE official_title LIKE '%fibrillation%'
This will work for you. If this is not what you want, let me know.

Error in use PostGIS SQL function with two select statement

I am new to PostGIS and SQL with a lot of things I need to learn.
I tried this SQL query:
Select ST_AsText(Select line from pathway1f)
I got errors.
Select line from pathway1f
This query works fine; thousands of lines are returned.
I want to decode the query result using ST_AsText().
It seems like I can't use two select statements. What's the correct syntax?
You can use the geometry column name directly
Select ST_AsText(line)
from pathway1f;

Sub-Queries in Sybase SQL

We have an application which indexes data using user-written SQL statements. We place those statements within parenthesis so we can limit that query to a certain criteria. For example:
select * from (select F_Name from table_1)q where ID > 25
Though we have discovered that this format does not function using a Sybase database. Reporting a syntax error around the parenthesis. I've tried playing around on a test instance but haven't been able to find a way to achieve this result. I'm not directly involved in the development and my SQL knowledge is limited. I'm assuming the 'q' is to give the subresult an alias for the application to use.
Does Sybase have a specific syntax? If so, how could this query be adapted for it?
Thanks in advance.
Sybase ASE is case sensitive w.r.t. all identifiers and the query shall work:
as per #HannoBinder query :
select id from ... is not the same as select ID from... so make sure of the case.
Also make sure that the column ID is returned by the Q query in order to be used in where clause .
If the table and column names are in Upper case the following query shall work:
select * from (select F_NAME, ID from TABLE_1) Q where ID > 25

SQL query where clause is too long

I've written an SQL query with a lot of 'or's in the 'where' clause:
i.e.
"SELECT * FROM myTable WHERE col1='a' or col1='b' or col1='c'...etc"
I'm trying to run a query in access via vb.net, but I keep getting "Query is too complex" error message.
I'm guessing I've hit some maximum limit. Anyone know a way around this, other than just to break it down into multiple queries?
How about using the IN operator instead?
SELECT Field1, Field2
FROM Table1
WHERE Field1 IN('Val1','Val2', ....)
If you query is that simple would you not be better using
SELECT * FROM myTable WHERE col1 in ('a','b','c')
but it would help to post the actual query so we can give a accurate answer
You could use SQL IN operator instead having multiple OR conditions.
You can use IN as in -
SELECT * FROM myTable WHERE col1 IN ('a','b','c','d');
Many have stated that you should use the IN-operator instead, but when that is used together with constants I believe the optimiser simply converts that to an OR-operator.
Instead you could load a temporary table with your constants and then use the IN-operator with that table.

SQL INSERT with sub query

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.
Heres what I came up with but I know is wrong..
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT #ModelId, VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId)
Essentially I want to provide the value for VehicleModelId through #ModelId, but it won't let me use it outside of the select statement.
Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT #ModelId,VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId