How can I look up column names in a SQL Server with an odd schema? - sql

I'm trying to select column names in our database and I'm unsure how. I tried some solutions I found on Stack Social but I haven't been able to make them work.
Is it even possible given the schema? I feel like it should be but my limited understanding in how to change the FROM clause is preventing me from doing this on my own. Below is a picture to help elaborate on the difficulty I'm having.
Picture of schema and attempts to return table and column names
EDIT: The problem is unique in that the overall layout of our database seems largely different from the norm. There seems to be nested databases and I wasn't sure how to use a specific DB.
The Use function worked well for this but its not intuitive from other answers. At least the picture can help someone in the future if they have a similar problem.

Use RatManreport
GO
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%CD%'

Related

I have a database full of many databases and Tables on Hive, now how could I search and find the column of interest?

Long in short that I realized that Hue(Hive/Impala) is not like Microsoft SQL server that you run the following to look for the Table of Interest.
Select * from information_schema.columns where column_name like '%The_Table_of_Interest%'
1st scenario: Imagine that I know what my Database is and I target my attention to the right table by searching through the table and find the column of interest.
2nd scenario: I don't know even what database I need to look for the right table and as a result the column of interest.
I realized that in Hue, there is no option to look for a column. All I can see is Table Search!
Having said that for the two above scenarios there should be a way to find the column of interest.
Scenario 2 is of course difficult to approach, however the 1st one looks a bit easier.
Now, I did my research and came of with running some code in Shell Command Line might be helpful to find the target column. However, that require some further investigation in the layer that I am not quite familiar.(Speaking of Metaset, etc.)
Therefore, here is my question.
Assume we are discussing the 1st scenario, now how can I search and find the columns while you have no knowledge about the tables at all. I can not take guesses and try every tables to find the right one to find the column that I am looking for. What would you suggest, and what is your strategy to approach? Thank you in advance. :)
Good Day H2019
Here are some commands that should help you out to explore the different tables that you have access to:
Find a table or a database
show tables like 'ben*'
Look at the table definition
show create table <table>;
Get table information
describe my_table_01;
Get even more information
describe extended table_name
Get more information in a pretty format
describe formatted table_name;
If you have access to Apache Ranger I also find it useful to look into tables permissions. (And see who's using what)
Apache Atlas if you use it it helpful to see where data comes from.(It keeps data lineage information and may help to give you an understanding of how things work)
Don't forget you can look at HDFS to find databases, tables if they're in /hive/warehouse/. This can also be helpful to understand when things are created.

Postgresql - why cant I compound queries on schemas properly?

Hi guys this is hopefully a simple one. I have only taken a simple SQL course and am now trying to apply it to a real postgres database. The immediate difficulty I am having is dealing with things at the schema level (multiple schemas) whereas everything I had learned only dealt with querying between multiple tables in a single schema.
I am trying to go one step more than this questioner and filter by schema, but things are behaving very weirdly to me
This is what I mean. This query returns the result I expect (and is lifted straight from this SO example):
so given my sql training so far I figure I can take the output and further refine it.
select table_name
from (select table_name, table_schema from information_schema.columns where column_name = 'user_id')
where table_schema = "web_app"
doing this gives a very weird error:
why is it treating what is obviously a field as a column?? what is this derived_table1? (presumably its the compounded query?)
in the extremely unlikely case that it is a software issue, i am using Navicat premium 9.1.2 and connecting to amazon redshift.
Related to the colours, I suppose it is related to your tool and the keywords are coloured like this. 'table_name' seems considered as a keyword. Check the list of keywords and correct it if necessary.
The query you have written is wrong. Please remove 'table_schema'.
If you select from a select, then the database engine considers the list of tables returned as derived. The list is computed at runtime and it is not easy to optimize those queries.
hi future people with the same issue, see a_horse_with_no_name's comment. thats the right answer for my issue.

Pervasive SQL query for entire database

The question was already asked here,
Pervasive SQL query
but never answered.
Can somebody help to create a query that will search the entire database for a specific value?
Sorry, I can't comment on the previous question as I am a new user and don't have enough reputation to do so.
There is not a built-in way to search every single column for a specific value.
I'm not exactly sure why you want to search every single column for a specific value. Seems a little excessive in terms of the performance hit on the database.
If you really need to do this, the best suggestion I can give would be to write a stored procedure that iterates all of the tables, then iterates all of the fields in each table to use them in the WHERE clause. A better way to do it would be to build the query using the fields where the value is like to be. For example, if you're trying to search all the tables for a specific ID, you probably don't need to search date or currency or quantity fields. How you do this will also depend on the version of PSQL you're using.
If you explain what you hope to accomplish and why you need it, we might be able to offer better suggestions.

Exploring HSQL database using SQL queries?

I'm using HSQL for the first time. I've previously used MySQL. It appears to lack MySQL commands such as SHOW TABLES. I will still need get information like that occasionally though. This is somewhat similar, but it returns more tables than I care for (not all my own):
SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_TYPE='TABLE'
So for that reason I would like to explore the meta tables being used by HSQL, but I don't know how. Could someone run me through the basics of how to explore an HSQL database to find out information about the overall structure. For example
How would I find out the column names of INFORMATION_SCHEMA.SYSTEM_TABLES?
How could I even have found INFORMATION_SCHEMA in the first place?
How do I find "siblings" to SYSTEM_TABLES?
How to get details about a column, like primary key and such?
etc.
Some of this information is covered in the HSQLDB Guide:
http://hsqldb.org/doc/2.0/guide/databaseobjects-chapt.html#dbc_standard_views_info_schema
The Standard view equivalent is INFORMATION_SCHEMA.TABLES and you can filter out the system views by adding WHERE TABLE_SCHEMA <> 'INFORMATION_SCHEMA' or a similar condition.
The column details are in INFORMATION_SCHEMA.COLUMNS, while primary keys are listed in KEY_COLUMN_USAGE.

How would you do give the user a preference for how from an SQL table is to be printed?

I'm given a task from a prospective employer which involves SQL tables. One requirement that they mentioned is that they want the name retrieved from a table called "Employees" to come in the form at of either "<LastName>, <FirstName>" OR "<FirstName> <MiddleName> <LastName> <Suffix>".
This appears confusing to me because this kind of sounds like they're asking me to make a function or something. I could probably do this in a programming language and have the information retrieved that way, but to do this in the SQL table exclusively is weird to me. Since I'm rather new to SQL and my familiarity with SQL doesn't exceed simple tasks such as creating databases, tables, fields, inserting data into fields, updating fields in records, deleting records in tables which meet a specific condition, and selecting fields from tables.
I hope that this isn't considered cheating since I mentioned that this was for a prospective employer, but if I was still in school then I could just outright ask a professor where I can find a clue for this or he would've outright told me in class. But, for a prospective job, I'm not sure who I would ask about any confusion. Thanks in advance for anyone's help.
A SQL query has a fixed column output: you can't change it. To achieve this. you could have a concatenate with a CASE statement to make it one varchar column, but then you need something (parameter) to switch the CASE.
So, this is presentation, not querying SQL.
I'd return all 4 columns mentioned and decide how I want them in the client.
Unless you have just been asked for 2 different queries on the same SQL table
You haven't specified the RDBMS, but in SQL Server you could accomplish this using Computed Columns.
Typically, you would use a View over the table..