Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [sql]

Structured Query Language is a language for interacting with relational databases. Read the tag wiki's guidelines for requesting SQL reviews: 1) Provide context, 2) Include the schema, 3) If asking about performance, include indexes and the output of EXPLAIN SELECT.

Filter by
Sorted by
Tagged with
4
votes
2answers
3k views

PHP SQLQuery class

I created a PHP class to handle building SQL query strings. I need advice on how to make the code more efficient. ...
3
votes
4answers
2k views

Shifting records in SQL Database while sorting with algorithm

I have a table in a SQL Server database, which holds information of some images, and the relevant gallery of them. The columns are like: ImageId, ...
0
votes
2answers
207 views

Query to compare execution performance metrics for the three most recent months

I've been working on a semi-awkward query in that it uses a very high number of functions given its relatively small size and scope. I was hoping to get some feedback on any ways I could format or re-...
2
votes
1answer
307 views

Newsletter SQL query optimization

I want to build a small newsletter like tool which sends mails in pre-defined timespans. First after registration, second 14 days later, third 7 days later etc. I came across for two database designs ...
1
vote
2answers
9k views

SQL query for adding column value to compare with other column

I have two tables ...
5
votes
1answer
268 views

SQL - How's my formatting?

I'm somewhat new to SQL (using it in the past but only being exposed to it heavily in my current role). Unfortunately nobody at my current company has really given me any advice on formatting. How can ...
2
votes
3answers
112 views

Query for combining results of same query running across multiple databases

I wrote a query to run the same query across multiple databases and combine the results. While it seems plenty quick I was wondering if there is a better way to do this. ...
2
votes
2answers
669 views

Phrases update query

My query is this: UPDATE `phrases` SET `phrases`.`count`=(SELECT COUNT(*) FROM `strings` WHERE `string` LIKE CONCAT('%', `phrases`.`phrase`, '%')) My tables look ...
6
votes
1answer
469 views

Database interaction logic

I have a database-intensive app, almost every activity requires access to the database, both reading and writing. I had a lot of problems with separate threads and activities on the stack closing ...
5
votes
2answers
149 views

A set of queries in PHP to show occupancy of various cabins

Okay so as I learn more about PHP and really strive to improve my code I have a few questions about a current setup of mine and how to approach it in the best way. I think I have all the tools ...
5
votes
2answers
3k views

Using a subquery to rename columns

This is a pattern in SQL queries that I've found myself repeating recently: ...
3
votes
3answers
227 views

Parsing Placeholders as SQL Arguments in PHP

I am trying to find a good way of parsing SQL arguments, the way I am accomplishing it now seems like it can use a lot of improvement. I am trying to convert arguments that are split apart by ', & ...
1
vote
1answer
2k views

How can I make this SQL Bit scalar function more efficient?

I'd like to make this SQL scalar function more efficient. How can I do that? ...
2
votes
3answers
2k views

Calling stored procedure with a sqlCommand

I have the following code as part of an sqlCommand which is calling a stored procedure: ...
3
votes
2answers
3k views

Relationship design for auto parts store database

I'm creating a design database for an autoparts store (this is a project school). For the moment, I'm not sure if the relationship model especially in MODEL, ...
5
votes
4answers
2k views

Database schema for Service Order Management project

I'm a student and I've created this MySQL database schema for my Service Order Management academic project. I want to know if I need to improve it and, if so, how I could do that. ...
4
votes
1answer
1k views

Execute multiple query from a text file

I am just wondering if this is a correct way to execute multiple query from a text file. ...
10
votes
1answer
1k views

SQL Server stored procedure boilerplate

What would you do to improve upon this boilerplate empty stored procedure, being mindful of the delicate balance between length, complexity, performance and clarity? ...
1
vote
2answers
4k views

Php INSERT class function

This is a INSERT function. It's working... I just pulled out the function from the class, atm to try it out. And improve. I think the code is a bit messy, And I am pretty sure I can improve the code, ...
2
votes
3answers
29k views

Inner join with first result

In SQL Server, there is two tables: Houses, and their images. I need a list with 20 houses with the first of their images (only one). I tried: ...
1
vote
1answer
252 views

Acquiring product details

I have these MySQL tables: ...
11
votes
5answers
4k views

T-SQL statement to breakdown UserAgent information

I have a table that contains the UserAgent string and a Count of how many times its seen. The T-SQL below is used to give a ...
3
votes
2answers
99 views

Upvote distribution SEDE query

There is a 1-to-many relationship between the posts table and the votes table, and they join on ...
3
votes
2answers
14k views

Splitting a string in an SQL query

BudgetCode is in the format 'xxxx-yyyyy-zzzzz'. This splits it correctly but I think that there has to be a more efficient way. ...
1
vote
2answers
259 views

Creating SQL queries for a MySQL database

I am writing a PHP application that works with a MySQL database. As I am fairly new to PHP I am looking for ways to improve my code. Below are two functions, ...
2
votes
1answer
220 views

Recommendations for stored procedure

Just wondering if I could solicit some feedback on a stored procedure I'm running and whether there's a more efficient way of handling the scenario (I'm pretty sure there will be!). Basically I have ...
6
votes
2answers
18k views

Read from SQL database table, store in list of objects

...
2
votes
1answer
218 views

Query of top players of the month

In a web game there is a ranking of the top points games played of the month: ...
11
votes
2answers
32k views

Insert multiple rows into a SQL table

I've written the following C# code to insert rows (that a user enters from a web application) into a SQL database. How does this code look? Is there a better, more efficient way to accomplish the ...
6
votes
10answers
63k views

Inserting records into a database

I wrote the following code to insert records into a database. The user selects the rows from the RadGrid and the insert command is executed when that user clicks ...
5
votes
1answer
202 views

Finding the use of the word 'Cthulhu' in tags on Stack Overflow

I've written the following SQL to count the number of times the name 'Cthulhu' turns up for each tag on Stack Overflow (original here): ...
1
vote
1answer
1k views

Cash Register database structure

I would like your thoughts on my database structure. I am kind of a rookie but I am trying. All of your suggestions, alternatives, ideas, additions are welcome. Thank you. ...
0
votes
2answers
1k views

Optimize slow SQL query

There is a application with a very slow query (listed below), and I have to optimize to run faster. The truth is that I don't know where to start. Any help? Thanks! ...
4
votes
3answers
267 views

I want a nice design pattern for “All”, “many” or “one”

In my particular usage here, I'm using two functions to deal with one thing. Lets say the user request comes in to select "ALL" the records, in which case I would pass on some code such as: ...
2
votes
1answer
874 views

Polling script to determine the continuous time a user has been connected

I've been asked to keep track of how long a user has been connected to a site without interruption. So far the solution I've come up with is to use ajax to poll the site every now and then to check ...
2
votes
4answers
398 views

Reduce the code in a WHERE clause without using dynamic SQL

How can I write the following T-SQL query part in a shorter way without using dynamic SQL? ...
9
votes
1answer
3k views

Database Class Creator

Overview I've written a class that will create C# code. The output is a single .cs file for each table in the default database (the database is defined inside a web.config file). I'm looking for a ...
6
votes
2answers
9k views

Database access class

I'm looking for any comments or feedback on my database access class. Security and speed are two things I'm most concerned about. One thing to note is this class has to work in a C# .NET 2 ...
3
votes
1answer
209 views

Maintaining a column for each datatype

I have a function that is suppose to store variables based on their datatype, and this is table I have come up with to store those variables into. Is it smarter to have everything broken down like ...
4
votes
2answers
207 views

Inner cursor performance issues [closed]

Before I tried to avoid using cursor because of performance issues. But now I have to do some calculations to reach some goals. I have a stored procedure that has a code like this: ...
8
votes
3answers
10k views

SQL: Search for a keyword in several columns of a table

I want to perform a search in several columns of a table. I use the following query: ...
4
votes
1answer
1k views

C++ template meta sql

I wrote a wrapper around boost-mpl and transformed it to an sql engine. It is a compile time sql thing, not related to normal databases. I have not finished it completely. I have some problem with the ...
8
votes
1answer
1k views

Idiomatic haskell database connectivity

In this post I asked about what would be idiomatic haskell database abstraction. I had been thinking for it a while, and the first answer was similar to what I had in mind, and I wrote a proof-of-...
6
votes
1answer
447 views

Retrieving the most recent communication from a user

Could someone review an accepted answered I gave on Stack Overflow? The use-case is as follows: Given a messaging system where a user can receive a message from a single user and send messages ...
7
votes
5answers
3k views

Accessing the only element of Java ResultSet

Does this code look OK? ...
5
votes
3answers
773 views

Get distinct of two columns

I want to get distinct of two columns from an SQL table. Can I optimize this query? ...
7
votes
5answers
5k views

Database schema for a school

Here is the database query needed to create the database: ...
4
votes
2answers
365 views

SQL paged search results

I'm trying to figure out a way to simplify implementing paged searching in sql server. My restrictions are: has to be in a proc. can't use dynamic sql. Here's my sample query: ...
3
votes
2answers
371 views

Database design for an online learning application

It is an online learning application with different types of learnable items, that different users learn. The users learn by spaced repetition (once right, wait a couple days, ask again), success is ...
7
votes
1answer
817 views

Avoiding sub-queries in named scopes

I use named scopes all over the place. When I have a named scope that deals with two different tables, I tend to query the second table in a sub-query. That way, if I mix and match with other tables ...