Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
8
votes1
answer279
viewsWhat is the best type in SQL to use with Cryptocurrency?
I am developing a system where I will store transactions of Cryptocurrencies, type Bitcoin, I can not in any way have problems of conversion and rounding, in C# I checked and the best is decimal,…
-
8
votes4
answers662
viewsHow to improve SQL performance with IN clause?
I have this SQL here: SELECT id, nome, url FROM categorias WHERE status = 1 AND id_ls IN (SELECT id_categoria FROM cliente_categorias) GROUP BY url What he does is seek only categories that have…
-
8
votes2
answers1132
viewsInsert sequence Oracle
Folks good night I’m wanting to insert data into a table , which has a sequence created, but when trying to insert data with nextval it presents the following error Error SQL Error: ORA-00911:…
-
8
votes2
answers555
viewsCommand SQL PARTITION BY?
I am reading and it was not very clear the command PARTITION BY in the creation of tables in structured databases (SQL), a basic example: CREATE TABLE employees ( id INT NOT NULL AUTO_INCREMENT…
-
8
votes2
answers1800
viewsIn which table is the foreign key in a ratio 1 to 1?
In an SQL relationship of two tables 1 to 1, there is some rule that defines which table the foreign key will be in?
-
8
votes2
answers2322
viewsHow to ignore accents in an SQL query?
How could I perform a query ignoring words with letters that contain accents? Example of words: Olá, é, Lógica, Pão I tried using the command collate noaccents as follows: select c.* from Curso c…
-
8
votes2
answers1771
viewsTurn columns into rows
I have the following structure: http://sqlfiddle.com/#! 9/738aa6/1 CREATE TABLE Viagem ( Idasemvolta int, Idacomvolta int ); INSERT INTO Viagem VALUES (64, 39) With the following select: select…
-
8
votes3
answers971
viewsLine with NULL value is disregarded by the SUM function?
I participated in the IFNMG competition, a test prepared by the CEFET Foundation. I solved the following question and marked the letter C, but the feedback says that the correct is the letter A.…
-
8
votes1
answer2652
viewsCommand "TRIM" SQL Server
I’m working with Sql Server, and I need to make a trim as a result of a consultation. I did it this way: select TRIM(NUMERO) from ENDERECO In the Sql Server Management the command is "recognized",…
-
8
votes3
answers931
viewsHow to query with Count
I have the table Upshot with thousands of data, Table Result NOME_CASA, NOME_OPONENTE, RESULTADO_FINAL, NOTA_FINAL. timeAzul timePreto 1 2 timeAzul timeBranco 2 2.10 timeBranco timeAzul 3 2.20…
-
7
votes4
answers4319
viewsString concatenation in SQL
I perform a query in a table in my Mysql database that returns the following fields to me: usuarioId nome sobrenome idade sexo escolaridade modeloNome hora Follow the sql below: SELECT…
-
7
votes3
answers408
viewsHow to index and update a user comparison system?
I have 2 tables with user characteristics and interests in a Mysql database with innoDB Tabela Carateristica id nome sobrenome cidade estadocivil altura peso fisico pele olhos filhos querofilhos…
-
7
votes2
answers5079
viewsHow to count records (SELECT COUNT) in Linq
How to count records that a query would return in Linq (Entity Framework) in C#? The equivalent in SQL is to do a SELECT COUNT(*). Note that I want to receive the count of records straight away,…
-
7
votes2
answers2661
viewsWhat are the advantages and disadvantages of using transaction explicitly
Beyond security what is the advantage of using explicit transaction? In which situations is recommended?
-
7
votes2
answers577
viewsIs it good practice to keep foreign key columns redundant?
Consider that we have the models A, B, C and D, each one referencing to the above, ie, A has many Bs, B has many Cs and C has many Ds: Redundancy-free A | id | ... | ------------ | ... | ... | B |…
-
7
votes3
answers2562
viewsSort SQL by 1,2,3 instead of 1,10,2,3
I have a name field in a table that has the following data: 1-Course 2-Course 10-Course When ordering by name it returns 1,10,2. How to make order to bring: 1,2,10 ? Ps: The field is NVARCHAR.…
-
7
votes3
answers151
viewsDoubt in select de sql
So ... I have a table that has a sequenc, one num, one dt_inicial and a dt_final. My question is: how to make one Select that takes the data between the dt_inicial and dt_final showing the num among…
-
7
votes1
answer266
viewsFinding Unknown Heroes in Stack Overflow using Data Explorer
I know there’s no Unknown Hero here at SOPT, but I know there are several at SOEN. My intention is to sort the percentage of responses accepted without votes of the people who hold this medal. And…
-
7
votes2
answers511
viewsFilters or Sort, which should be the first when creating an index in the database
Given, for example, the following Query: SELECT ClienteId, Nome, DataNascimento, Cidade, Estado, DataCadastro FROM Cliente WHERE Estado = :Estado AND Cidade = :Cidade ORDER BY DataCadastro,…
-
7
votes2
answers1017
viewsPrevent DROP TABLE
I would like to prevent deletions on a table in Sqlite. CREATE TRIGGER nao_deletar_tabela BEFORE DELETE ON tabela BEGIN SELECT RAISE(IGNORE); END; It worked! Whenever one runs DELETE, nothing…
-
7
votes2
answers6186
viewsFunction equivalent to "DISTINCT ON" in Oracle BD?
In Postgres, you can make distinct apply to only 1 select field using the DISTINC ON, how can I reproduce this function in an Oracle database? Follow an example query: SELECT DISTINCT ON (CITY)…
-
7
votes2
answers1014
viewsSetting up a database, how do I make this relationship?
I’m assembling the EER diagram of an ERP. Almost all tables have a column called "id_company" that relates to the "Companies" table. This is because the user will be able to manage one or more…
-
7
votes1
answer185
viewsWhy does this query return *?
Why this query returns to me *? select convert(varchar(2), 141) I thought it was the ascii table, but the 141 doesn’t match the *. Utilise SQL Server 2008 R2…
sqlasked 9 years, 9 months ago Diego Zanardo 3,301 -
7
votes1
answer1339
viewsA Product in Multiple Categories - PHP
I would like to know how to relate a product in several categories ? For example: A shirt can be in the Categories : "Green / Blue / Yellow" and when I filtered by category the product should appear…
-
7
votes2
answers18011
viewsINNER JOIN with two fields of the same table
I’m having a problem I haven’t found a solution to so far. I have 3 tables in Mysql: Units:containing information from "stores". Orders: contain the order information made by loja01 for loja02, both…
-
7
votes2
answers4428
viewsTable to record history record (Best way)?
I am doubtful what would be the best way to create a table so that each action of a record is saved thus creating a history of it. I have the following table, but I don’t know if it’s the best.…
-
7
votes1
answer481
viewsPopulate aggregate class from datatable using a Dataset with LINQ C#
I’m having trouble filling an aggregate class in C#. Is it possible using LINQ? Does anyone have any example of how to solve this problem using the return of a DataTable or DataSet? public class…
-
7
votes3
answers660
viewsLINQ corresponding to SQL with clause LIKE 'XXX%'
I am using the following LINQ to return a query: .Where(c => c.Nome.Contains(nome)).OrderBy(c => c.Nome) But if the name is Maria, he brings: José Maria Maria José How to bring organized as if…
-
7
votes1
answer1273
viewsIf the field has DEFAULT it must be NOT NULL?
Situation I was adding a few more columns to a table and falls from this thought. By default here we leave all fields can have value NULL, however if he has a DEFAULT he will insert the DEFAULT in…
-
7
votes1
answer76391
viewsINNER JOIN with 3 tables
I have 3 tables Tb_contratocotista: Tb_contract: Tb_stakeholder: I would like to relate the values of the table Tb_contract with Tb_stakeholder, but the way I got it only returns the result of…
-
7
votes1
answer713
viewsDoes number of columns influence performance?
When modeling a database, the number of columns can interfere with performance? Ex: Tabela1 possui 2 campos, 1 int pk e 1 nvarchar(50) Tabela2 possui 50 campos, 1 int pk e 49 nvarchar(50) Select…
-
7
votes1
answer73
viewsGroup by with subdivisions
I have an SQL that brings the number of people who graduated on a given date. Let’s say that of 300 people, 25 graduated on '2010-06-27'. I would like to include two more columns that return the…
-
7
votes1
answer451
viewsWhat is the advisable way to mount a query?
What is the advisable way to mount a query in C#? It is advisable to use concatenation with the operator +? For example: query += " WHERE nome LIKE '%" + nome + "%'"; Here follows a query I set as…
-
7
votes2
answers4071
views -
7
votes1
answer153
views -
7
votes2
answers4076
viewsSimple example of how to generate an XML file on Oracle
One would have a simple example of a PLSQL Procedure (or documentation location) to generate an XML file based on a Schema (xsd), the data would be generated from an SQL query. In reality archives…
-
7
votes3
answers2045
viewsInserting record and recovering ID generated by SQL Server 2012
I want to insert a record into a table, recover the ID that was generated by AUTO_INCREMENT and insert records into other tables using this ID. But, I want to do this all using the BeginTransaction.…
-
7
votes2
answers642
viewsError while trying to update table using C#
I created a form to change a table, but my update is not working. cnxCli.sel =/*"set dateformat dmy \n"+ */ "update Cliente" + "set Nome = '" + txtNome.Text +"'," + "Rg = '" + mskRg.Text + "'," +…
-
7
votes1
answer2296
viewsRounding as a result of multiplication
I have this scenario: - multiply by 2 values which will be rounded to 2 boxes for the table to be inserted. Follow the example: CREATE TABLE #TMP ( A DECIMAL(23,6), B DECIMAL(28,2) ) INSERT INTO…
-
7
votes3
answers1297
viewsHow to delete the last record from a table?
I was using the following code in mysql: delete from aluno where id=(select MAX(id) from aluno); But the following error appears: Error Code: 1093. You can’t specify target table 'student' for…
-
7
votes3
answers280
viewsLog tables should have Foreign Keys?
I had a case in my service these days when it was not possible to delete a record because it was referenced by the table of log. Logically what generated a call for the IT area to solve. But that…
-
7
votes5
answers2439
viewsSubtract sums of fields in two separate tables
I need to add the value of a column in SQL, so it looks like this: SELECT SUM(qtdsaco) FROM armazem This code works. Now I need to add the value of the other table, here it is: SELECT SUM(qtdsaco)…
-
7
votes2
answers611
viewsSuggestion Migration System and modeling of individuals and legal entities in system with Entity Framework
Well, here’s the thing. I have a database that has the purpose of making a company’s ledger, basically this bank has a table CaixaCorrido with the columns Id, PessoaNome, DataHora, Descricao, Valor,…
-
7
votes2
answers566
viewsInsert into multiple input fields in different tables
I have the date fields, CNPJ, latitude, longitude, site, facebook, on the same screen, only are 3 different tables, how do I give the insert in them from the same button?? What do I use to save the…
-
7
votes1
answer131
viewsSearch in two Mysql tables
Is it possible to "select" by merging two tables in Mysql? for example: I have a table called "favorite" that saves the id_da_postagem and the id_do_usuario. the idea would be to go through the…
-
7
votes5
answers4473
viewsIn the ORDER BY of a SELECT change a comma for a period
I have a table in MYSQL (can be seen here http://sqlfiddle.com/#! 9/187ce/1 ), where the price is with commas and not points: 2.00 2,33 11.00 When I’m going to give an order by: ORDER BY preco ASC…
-
7
votes1
answer776
viewsMysql very slow sorting for results with many records
I have two tables, the first is used to group the data of the second with some information about the set, in order to facilitate the pagination of the contents. Table 1: - id INT(11) NOT NULL…
-
7
votes3
answers1514
viewsSearch between dates with between
I have the following research: SELECT * FROM (`agendamentos`) WHERE `age_data_agendado_para` BETWEEN "2016-08-28" and "2016-08-30" In the database, I have: But on my return, I can only search for…
sqlasked 8 years, 3 months ago Sr. André Baill 6,946 -
7
votes2
answers12161
viewsDifference between RIGHT JOIN and LEFT JOIN
I have a little knowledge in relationship tables, read the answers to the question about What is the difference between INNER JOIN and OUTER JOIN?, and understood, theoretically. LEFT JOIN Returns…
-
7
votes1
answer482
viewsReturn last value inserted in table where value is not Identity?
I would like to return the last value of the primary key entered in the table, but my primary key is CPF so I can’t use SELECT SCOPE_IDENTITY().