Sql

Structured Query Language (SQL) is a language for querying databases. Questions should include code examples, table structure, sample data, and a tag for the DBMS implementation (e.g. MySQL, PostgreSQL, Oracle, MS SQL Server, IBM DB2, etc.) being used. If your question relates solely to a specific DBMS (uses specific extensions/features), use that DBMS's tag instead. Answers to questions tagged with SQL should use ISO/IEC standard SQL.

63 votes
24 answers
2.6K views
Search text in stored procedure in SQL Server

I want to search a text from all my database stored procedures. I use the below SQL: SELECT DISTINCT o.

avatar
Scot
0 1 0 9
asked 07 September, 2021
48 votes
20 answers
614 views
Join vs. sub-query

I am an old-school MySQL user and have always preferred JOIN over sub-query. But nowadays everyone uses sub-qu

avatar
Charis
0 1 0 9
asked 07 September, 2021
61 votes
17 answers
1.2K views
Save PL/pgSQL output from PostgreSQL to a CSV file

What is the easiest way to save PL/pgSQL output from a PostgreSQL database to a CSV file? I'm using Pos

avatar
Dorinda
0 1 0 9
asked 07 September, 2021
54 votes
11 answers
1.1K views
Insert into a MySQL table or update if exists

I want to add a row to a database table, but if a row exists with the same unique key I want to update the row

avatar
Rudy
0 1 0 9
asked 07 September, 2021
63 votes
15 answers
1.4K views
When should I use CROSS APPLY over INNER JOIN?

What is the main purpose of using CROSS APPLY? I have read (vaguely, through posts on the Internet) that cros

avatar
Sena
0 1 0 9
asked 07 September, 2021
54 votes
12 answers
2.7K views
INNER JOIN ON vs WHERE clause

For simplicity, assume all relevant fields are NOT NULL. You can do: SELECT table1.this, table2.that, ta

avatar
Percy
0 1 0 9
asked 07 September, 2021
61 votes
25 answers
3K views
SQL update from one Table to another based on a ID match

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to

avatar
Boyd
0 1 0 9
asked 07 September, 2021
58 votes
30 answers
2.5K views
Parameterize an SQL IN clause

How do I parameterize a query containing an IN clause with a variable number of arguments, like this one? SEL

avatar
Kurtis
0 1 0 9
asked 07 September, 2021
57 votes
14 answers
985 views
How do I escape a single quote in SQL Server?

I am trying to insert some text data into a table in SQL Server 9. The text includes a single quote '. Ho

avatar
Kristopher
0 1 0 9
asked 07 September, 2021
54 votes
30 answers
1.2K views
Retrieving the last record in each group - MySQL

There is a table messages that contains data as shown below: Id Name Other_Columns ----------------------

avatar
Janella
0 1 0 9
asked 07 September, 2021
65 votes
14 answers
3.1K views
How do I limit the number of rows returned by an Oracle query after ordering?

Is there a way to make an Oracle query behave like it contains a MySQL limit clause? In MySQL, I can do this:

avatar
Vi
0 1 0 9
asked 07 September, 2021
50 votes
24 answers
1.7K views
Get list of all tables in Oracle?

How do I query an Oracle database to display the names of all tables in it?

avatar
Freida
0 1 0 9
asked 07 September, 2021
57 votes
6 answers
2.6K views
Difference between JOIN and INNER JOIN

Both these joins will give me the same results: SELECT * FROM table JOIN otherTable ON table.ID = otherTable.

avatar
Scot
0 1 0 9
asked 07 September, 2021
60 votes
14 answers
3.1K views
SQL Server - Best way to get identity of inserted row?

What is the best way to get IDENTITY of inserted row? I know about @@IDENTITY and IDENT_CURRENT and SCOPE_IDE

avatar
Dorinda
0 1 0 9
asked 07 September, 2021
65 votes
3 answers
2K views
Using group by on multiple columns

I understand the point of GROUP BY x. But how does GROUP BY x, y work, and what does it mean?

avatar
Dusty
0 1 0 9
asked 07 September, 2021
60 votes
17 answers
1.4K views
How can I list the tables in a SQLite database file that was opened with ATTACH?

What SQL can be used to list the tables, and the rows within those tables in an SQLite database file - once I

avatar
Janella
0 1 0 9
asked 07 September, 2021
52 votes
13 answers
2.5K views
Can I concatenate multiple MySQL rows into one field?

Using MySQL, I can do something like: SELECT hobbies FROM peoples_hobbies WHERE person_id = 5; My Output:

avatar
Renaldo
0 1 0 9
asked 07 September, 2021
55 votes
24 answers
1.9K views
How to reset AUTO_INCREMENT in MySQL

How can I reset the AUTO_INCREMENT of a field? I want it to start counting from 1 again.

avatar
Scot
0 1 0 9
asked 07 September, 2021
63 votes
7 answers
2.1K views
What are the options for storing hierarchical data in a relational database?

Good Overviews Generally speaking, you're making a decision between fast read times (for example, nested

avatar
Leonardo
0 1 0 9
asked 07 September, 2021