Questions tagged [query]

Writing queries to request or change information stored in a database or other information management system. Questions should include table and index definitions, sample data, expected output, and a tag for the specific system (e.g. MySQL, PostgreSQL, Oracle, SQL Server, MongoDB).

1566 questions
41
votes
5 answers

How to select distinct for one column and any in another column?

I need to query an SQL database to find all distinct values of one column and I need an arbitrary value from another column. For example, consider the following table with two columns, key and value: key value === ===== one test one …
WilliamKF
  • 521
  • 2
  • 6
  • 6
6
votes
5 answers

SQL Pair Query?

I have only one table which contains 3 columns for travel company. It shows buses that went from City A to City B etc. I would like to find how many times this route has been used. I can easily find one way from A to B but I want this program to sum…
sqlquery
  • 81
  • 1
  • 1
  • 2
5
votes
4 answers

How do I go about creating this SQL query?

I'm attending the free DB course at Stanford Online, and frankly one of the exercises gave me some trouble. I have a feeling that this should be horribly simple, so for a DBA, I'm obviously not very good with SQL. We're working with a simplified…
Roy
  • 1,060
  • 5
  • 16
  • 39
3
votes
1 answer

Multiple Distinct Columns

I have a table named 'DATAENTRY' with the following columns Title FName MName LName Add1 Add2 Add3 City Now I want to select records which have different (FName, MName ,LName) so I tried SELECT DISTINCT FName, MName, LName FROM dataentry The…
user71538
3
votes
2 answers

how to GROUP BY on specific column and COUNT() the other columns?

I've queried a few tables and result is as below: I want to count correct col upon ct_id(course) col. I queried a lot but gained no success. How to query such a thing? for example in SELECT I should have two columns: -correctNo -incorrectNo for…
Alireza
  • 3,636
  • 10
  • 37
  • 44
3
votes
3 answers

SQL Query Against Previous SQL Query Results

Is there an SQL database (for C#) that allows me to query against on-disk database and then later on (probably offline at client side) query against the in-memory result set using SQL? I basically want to always be able to SQL query against whatever…
geeko
  • 21
  • 1
  • 12
2
votes
1 answer

How to see results from procedure still running after disconnect

I executed a stored procedure that returns a SQL statement under 'Messages' tab after completion. This query window in SSMS is now showing disconnected and has "transport-level error has occurred" in results frame. I can see the procedure is still…
2
votes
0 answers

QBE Query By Example. How does it handle OR conditions?

I've been looking at implementing QBE into an application but need more information on how more complex queries work. How does QBE handle "OR" conditions? For example when searching a telephone book how would QBE work for a query needing the…
Karlth
  • 133
  • 4
2
votes
1 answer

All columns except selected

Is it possible to select all fields except few fields for a single table? (Suppose there are bundle of fields that writing each field in Select statement is time consuming). id row_num customer_code comments ----------------------------------- 1 …
user175623
  • 23
  • 4
2
votes
2 answers

How to check for record which has no entry in another table with some specific value?

For example, if we have two tables: Table1 and table 2 Table1 - main table (with unique entry for all records) table2 - is a history table(where is containg many entries for same record on time stampe basis) (also contains some specific field…
thiyaga rajan
  • 31
  • 1
  • 1
  • 2
2
votes
1 answer

Query logical processing phases while mixing old and new join syntax

Could anyone clarify why order of tables in FROM matters if I mix old and new syntax for joining tables? For example, SELECT * FROM table1 a, table2 b INNER JOIN table3 c ON (c.a_id = a.id) -- error - a.id - invalid identifier WHERE (a.id =…
a1ex07
  • 9,000
  • 3
  • 24
  • 40
2
votes
1 answer

Dividing values from a single column into 2 based on a rule

I am trying to create an SQL query to divide into 2 separate columns based on a rule. My table is as follows Name | Item ID | Parent ID ----- | -------- | ---------- A | 1 | Null B | 2 | Null C | 3 | Null D …
mrarvind901
  • 21
  • 1
  • 3
1
vote
3 answers

How to retrieve data of two columns from two tables?

I have two Table MATCH_MASTER and TEAM_MASTER MATCH_MASTER |---------------------------| |M_ID | TEAM1_ID | TEAM2_ID | |---------------------------| | 1 | 1 | 2 | TEAM_MASTER |T_ID | T_NAME | |--------------| | 1 | A | | 2 …
Gani
  • 113
  • 3
1
vote
1 answer

SQL query question

I want to find the records of August birthdays in own table.I'm using Datetime Field. Sample record: 01.08.2001 06.06.1985 26.08.1995 etc.. How should I do write a query? Regards,
Cell-o
  • 1,088
  • 8
  • 21
  • 37
1
vote
0 answers

What is a 'clean' way of getting the output required?

I need to aggregate the total for when the case when statement returns true. But I need to do this per partition. My data looks something like this: Product|Variation_code|Total_product_variations|Discount_code A | x21 | 3 …
Laura
1
2 3